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

MSSQL: Fix error 'Consistency validation for SQL Server registry keys'

While installing Microsof SQL Server 2012, I ran into the following error. "The SQL Server registry keys from a prior installation cannot be modified. To continue, see SQL Server Setup documentation about how to fix registry keys." How to fix Replace the D:\x64\FixSqlRegistryKey_x86.exe file with the original file from the ENU\SQLFULL_ENU.iso file downloading from Microsoft Download Center. This ISO file is 4.2GB. So you can click FixSqlRegistryKey_x86.exe to download this file only. After replacing that file, press Re-run to check. All rules are passed.

Windows command: icacls

Icacls is a command-line utility that can be used to display or modify discretionary access control lists (DACLs) on specified files, and apply stored DACLs to files in specified directories.

Microsoft Photos App

Reset, uninstall and Reinstall Microsoft Photos app, the default photo viewer on Microsoft Windows 10.