Skip to main content

Powershell: Commonly used command lets

Get Things Done with Powershell. Notes on daily using Powershell.

$PSVersionTable : Get Powershell version
New-Guid
Get-Alias / gal
Get-Verb
Get-Member
Get-Item / gi
Set-Item / si
Get-ChildItem / gci

Help-Update

Get the last argument with $$

Example: Create new folder lthwpowershell, then change into that newly created folder

PS F:\Projects> New-Item -Type Directory lthwpowershell


    Directory: F:\Projects


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----        6/23/2019   2:28 PM                lthwpowershell


PS F:\Projects> Set-Location $$
PS F:\Projects\lthwpowershell>

On Linux, we use $_ instead.

Generate GUID with New-Guid

PS C:\Users\Tuyen> new-guid

Guid
----
9297476d-2357-4e5d-896b-569613b2865f


PS C:\Users\Tuyen>


Get-ADUser -Filter * -Properties * | Select-Object -Property * | Export-Csv -Path C:\Audit\Users.csv -NoTypeInformation
$currentdate = Get-Date -Format yyyyMMdd
$server = "dc.ipractice.com.vn"
$domain = "ipractice"
Get-ADUser -Server $server -Filter * -Properties * | Select-Object -Property * | Export-Csv -Path C:\Audit\Users-$domain-$currentdate.csv -NoTypeInformation
$server = "dc-pr.hanoi.ipractice.com.vn"
$domain = "hanoi"
Get-ADUser -Server $server -Filter * -Properties * | Select-Object -Property * | Export-Csv -Path C:\Audit\Users-$domain-$currentdate.csv -NoTypeInformation
$server = "dchaiphong.haiphong.ipractice.com.vn"
$domain = "haiphong"
Get-ADUser -Server $server -Filter * -Properties * | Select-Object -Property * | Export-Csv -Path C:\Audit\Users-$domain-$currentdate.csv -NoTypeInformation
$server = "dcnghean.nghean.ipractice.com.vn"
$domain = "nghean"
Get-ADUser -Server $server -Filter * -Properties * | Select-Object -Property * | Export-Csv -Path C:\Audit\Users-$domain-$currentdate.csv -NoTypeInformation
$server = "dcdanang.danang.ipractice.com.vn"
$domain = "danang"
Get-ADUser -Server $server -Filter * -Properties * | Select-Object -Property * | Export-Csv -Path C:\Audit\Users-$domain-$currentdate.csv -NoTypeInformation
$server = "dchcm.hcm.ipractice.com.vn"
$domain = "hcm"
Get-ADUser -Server $server -Filter * -Properties * | Select-Object -Property * | Export-Csv -Path C:\Audit\Users-$domain-$currentdate.csv -NoTypeInformation

Run command one by one, skip the rest if one command return false: &&

echo 1 && echo "sleep 5 seconds" && sleep 5 && echo 2

Run command in backgroud: &

sleep 10 &

Check backgroud job

job

Popular posts from this blog

IIS: Delete cached files on server running IIS

Delete cached files on server running IIS When changing css, javascript files, check to delete if IIS still caches old files in the default folder C:\inetpub\temp\IIS Temporary Compressed Files\<sitename>\$^_gzip_D^\ Apply to: IIS 8.5

Linux command: du - disk usage

Where have all my storage gone? du summarize disk usage of the set of FILEs, recursively for directories.

ManageEngine ServiceDesk Plus - Reset password

Let's reset the default administrator's password to 'admin'