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 a specific name (in the example, containing "dev" in a name)

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

Get EC2 instances without any IAM roles attached:

(Get-EC2Instance).Instances | ? { !$_.IamInstanceProfile.Arn } | select @{n="Name";e={($_.Tag | ? Key -eq "Name").Value}}, InstanceId, PrivateIpAddress, { $_.IamInstanceProfile.Arn }

Get EC2 instances count grouped by family/type:

# Get a list of all instances
$instances = (Get-EC2Instance).Instances

# Show all instances
$instances | group InstanceType | select Name, @{n='Total';e={$_.Count }}, @{n='Running';e={($_.Group | ? { $_.state.Name -eq "running" }).Count }}, @{n='Stopped';e={($_.Group | ? { $_.state.Name -eq "stopped" }).Count }}

# Show all instances grouped by family
$instances | group { $_.InstanceType.ToString().Split('.')[0] } | select Name, @{n='Total';e={$_.Count }}, @{n='Running';e={($_.Group | ? { $_.state.Name -eq "running" }).Count }}, @{n='Stopped';e={($_.Group | ? { $_.state.Name -eq "stopped" }).Count }}

# Show all instances grouped by type
$instances | group { $_.InstanceType.ToString().Split('.')[1] } | select Name, @{n='Total';e={$_.Count }}, @{n='Running';e={($_.Group | ? { $_.state.Name -eq "running" }).Count }}, @{n='Stopped';e={($_.Group | ? { $_.state.Name -eq "stopped" }).Count }}

Remove unused AMI images:

$usedamis = ((Get-EC2Instance).Instances | select ImageId -unique).ImageId
Get-EC2Image -Owner "_your_owner_id_" | ? ImageId -notin $usedamis | Unregister-EC2Image

Get a list of unused subnets:

Get-EC2Subnet | ? SubnetId -notin (Get-EC2Instance).Instances.SubnetId | select AvailabilityZone, VpcId, SubnetId, CidrBlock