PowerShell one-liners cheat sheet


PowerShell one-liners cheat sheet

Over 10 years of automating daily routine tasks, quite a few short scripts have been collected to perform various tasks with Active Directory, Cisco UCS, NetApp, VMWare vSphere/vCenter, etc.

And I also found an interesting video on Channel9 about combining commands into one line.

And here's my PowerShell one-liner cheat sheet:

Get PowerShell version:

Get-Host | Select Version

Get your public IP address:

(Invoke-RestMethod ipinfo.io/json).ip

Get Windows uptime (-Since parameter will show you last boot time):

Get-Uptime -Since

Enable TLS 1.2 support:

[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12

Get Active Directory latest shadow copy backup date:

Get-WinEvent -FilterHashtable @{Logname='Directory Service';ID=1917}

Get a list of disabled users from Active Directory

Search-ADAccount -UsersOnly -AccountDisabled

Send an email:

Send-MailMessage -From 'bob@outlook.com' -To 'sam@outlook.com', 'john@outlook.com' -Subject 'Bob CV' -Body 'Here is my CV' -Attachments .\cv.pdf -Priority High -DeliveryNotificationOption OnSuccess, OnFailure -SmtpServer 'smtp-mail.outlook.com' -port 587 -UseSsl

Get a list of all non-Windows servers that have not contacted Active Directory in the last 30 days

Get-ADComputer -Filter { OperatingSystem -notlike "Windows Server*" } -Properties PasswordLastSet | ? { (((Get-Date) – $_.PasswordLastSet).Days) -gt 30} | Select Name, @{N="Age";E={ (((Get-Date) – $_.PasswordLastSet).Days) }}

Copy item to the clipboard

Get-Childitem | Set-Clipboard

Get EC2 instances with specific name

(Get-EC2Instance -Filter @{Name = "tag:Name"; Value = "*dev*"}).Instances | select @{n="Name";e={($_.Tag | ? Key -eq "Name").Value}}, PrivateIpAddress