Tuesday, January 28, 2025

More Power shell One Liners

PowerShell One-Liners Permalink
Time of the Last Reboot Permalink

(Get-CimInstance Win32_OperatingSystem).LastBootUpTime

Find Your Public IP Address Permalink

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

Find Domain Controllers on Your Domain Permalink

Resolve-DnsName -Type ALL -Name _ldap._tcp.dc._msdcs.$env:userdnsdomain

List Software Available for Uninstall Permalink

Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | Format-Table

Install PowerShell Core (6 and 7) Permalink

Invoke-Expression "& { $(Invoke-RestMethod -Uri aka.ms/install-powers…) }" -UseMSI -Preview

Get Free Space for System Drive Permalink

(Get-PSDrive $Env:SystemDrive.Trim(':')).Free/1GB

Get Parent Process(es) Permalink

foreach ($prid in ($ppid = foreach ($process in (Get-Process -Name "powershell")) { (Get-CimInstance Win32_Process | Where-Object processid -EQ $process.Id).parentprocessid })) { Get-Process -Id $prid }

List Subdirectories in the Current Directory Permalink

Get-ChildItem -Directory

List Started Services Permalink

Get-Service | Where-Object {$_.status -eq "Started"}

Tail a File Permalink

Get-Content ./logfile.log -Tail 5 –Wait

Port Scanner Permalink

0..65535 | Foreach-Object { Test-NetConnection -Port $_ scanme.nmap.org -WA SilentlyContinue | Format-Table -Property ComputerName,RemoteAddress,RemotePort,TcpTestSucceeded }

Common WMI (CIM) Queries Permalink

# BIOS Version
(Get-CimInstance Win32_BIOS).SMBIOSBIOSVersion

# Serial Number
(Get-CimInstance Win32_BIOS).SerialNumber

# Model
(Get-CimInstance Win32_ComputerSystem).Model

# Printers
Get-CimInstance Win32_Printer | Select-Object Name, PortName, Default

# Active Directory Domain
(Get-CimInstance Win32_ComputerSystem).Domain

Get Time Until Next Year Permalink

(Get-Date -Date "$((Get-Date).Year + 1)/1/1") - (Get-Date)

Cat Facts Permalink

Invoke-WebRequest -Uri 'https://catfact.ninja/fact' -UseBasicParsing | Select-Object -ExpandProperty 'Content' | ConvertFrom-Json | Select-Object -ExpandProperty fact

Get a Random XKCD Comic Permalink

Invoke-RestMethod "http://xkcd.com/$(Get-Random -min 0 -max 2000)/info.0.json" | Select-Object title, transcript, alt, img | Format-List

Conclusion Permalink

Hopefully, you’ve found some snippets in this post useful. For me, one-liners have been fun, interactive ways to play with PowerShell and test my knowledge, expectations, and push the language to its limits.

Have a cool one-liner you’d like to add to this doc? Feel free to drop a comment or edit this page directly on GitHub using the button below.

Want more one-liners? Click here.

No comments:

Post a Comment

Current Project

More Power shell One Liners

PowerShell One-Liners Permalink Time of the Last Reboot Permalink (Get-CimInstance Win32_OperatingSystem).LastBootUpTime Find Your Public IP...