PowerShell one-liners cheat sheet
PowerShell is a powerful and flexible command-line shell and scripting language used by IT professionals and power users. One of its greatest strengths is the ability to perform tasks with just a single line of code. This "cheat sheet" article provides an overview of some of the most useful PowerShell one-liners for administrators, developers, and other technical professionals. Whether you're a seasoned PowerShell expert or just getting started, this article is an essential resource for anyone looking to streamline their work and increase their efficiency. With concise, easy-to-remember examples and explanations, this cheat sheet is the perfect tool for anyone looking to get the most out of their PowerShell experience.
Use these snippets as starting points and test them in a non-production shell first. If you need a repeatable sandbox, the PowerShell in Docker guide shows how to preload modules into a container, and the EBS encryption guide includes AWS examples that pair well with the EC2 commands below.
I also found an older Microsoft video about combining commands into one line.
And here is my PowerShell one-liner cheat sheet:
Get PowerShell version:
Get-Host | Select VersionGet your public IP address:
(Invoke-RestMethod ipinfo.io/json).ipGet Windows uptime (-Since parameter will show you last boot time):
Get-Uptime -SinceEnable TLS 1.2 support:
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12Get 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 -AccountDisabledSend 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 -UseSslGet 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-ClipboardGet 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}}, PrivateIpAddressGet 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-EC2ImageGet a list of unused subnets:
Get-EC2Subnet | ? SubnetId -notin (Get-EC2Instance).Instances.SubnetId | select AvailabilityZone, VpcId, SubnetId, CidrBlockGet a hard drive temparature (run as Administrator):
Get-PhysicalDisk | select FriendlyName, @{N="Temperature"; E={($_ | Get-StorageReliabilityCounter).Temperature}}