Skip to content

Instantly share code, notes, and snippets.

@ngrogg
Last active June 26, 2024 12:37
Show Gist options
  • Save ngrogg/8c18d2bbdae95461dca54a250a31a6e0 to your computer and use it in GitHub Desktop.
Save ngrogg/8c18d2bbdae95461dca54a250a31a6e0 to your computer and use it in GitHub Desktop.
Some useful PowerShell commands

Some useful PowerShell commands

Check disk use (Windows), run in PowerShell as admin:
Get-ChildItem c:\ -r -ErrorAction SilentlyContinue –Force |sort -descending -property length | select -first 10 name, DirectoryName, @{Name="MB";Expression={[Math]::round($_.length / 1MB, 2)}}
gci C:\inetpub -r| sort -descending -property length | select -first 10 name, @{Name="GB";Expression={[Math]::round($_.length / 1GB, 2)}}

Get subdirectory size in PowerShell:
$colItems = Get-ChildItem C:\ -ErrorAction SilentlyContinue -Force | Where-Object {$_.PSIsContainer -eq $true} | Sort-Object
foreach ($i in $colItems)
{
$subFolderItems = Get-ChildItem $i.FullName -ErrorAction SilentlyContinue -recurse -force | Where-Object {$_.PSIsContainer -eq $false} | Measure-Object -property Length -sum | Select-Object Sum
$i.FullName,+ " -- " + "{0:N2}" -f ($subFolderItems.sum / 1GB) + " GB"
}

Find a file PowerShell:
Get-Childitem –Path C:\ -Include FILENAME -File -Recurse -ErrorAction SilentlyContinue
^^ Accepts wildcards

Check server timezone, PowerShell
Get-TimeZone

PowerShell find file (ala linux find)
Get-ChildItem -Path LETTER:\path\to\folder -Filter FILENAME -Recurse -ErrorAction SilentlyContinue -Force

PowerShell, log user out
quser to list user
logoff ID

PowerShell, list server public IP
(Invoke-WebRequest ifconfig.me/ip).Content.Trim()

PowerShell, lock user
Disable-LocalUser USERNAME

PowerShell, find PID using port:
Get-Process -Id (Get-NetTCPConnection -LocalPort PORT).OwningProcess

Get members of group, PowerShell
Get-LocalGroupMember -Name GROUP
Get-LocalGroupMember -Name "Remote Desktop Users"

PowerShell, check last login for user
Get-LocalUser $i | Format-List -Property LastLogon

PowerShell, grep
COMMAND | Select-String "VALUE"
Ex.
Get-NetTCPConnection -State Listen | Select-String "3389"

PowerShell, nmap
Test-NetConnection -ComputerName IP -Port PORT

PowerShell check server uptime:
Get-CimInstance -ClassName Win32_OperatingSystem | Select LastBootUpTime

PowerShell check user details,
Get-LocalUser -Name USERNAME | Format-List

PowerShell, restart service (run in admin terminal)
Restart-Service -Name SERVICE

PowerShell, get service status
Get-Service -name SERVICE

PowerShell, open admin Terminal
Start-Process PowerShell -verb runas

PowerShell find OS info,
systeminfo | findstr /B /C:"OS Name" /B /C:"OS Version"

Sample output:
PS C:\WINDOWS\system32> systeminfo | findstr /B /C:"OS Name" /B /C:"OS Version"
OS Name: Microsoft Windows Server 2019 Datacenter
OS Version: 10.0.17763 N/A Build 1776

Windows vimrc,
C:\Users\USERNAME\_vimrc

PowerShell find RC file,
echo $profile

PowerShell add aliases,
New-Alias ALIAS COMMAND

PowerShell add aliases w/ spaces
function ALIAS {Command to run}

PowerShell view PID using port,
Get-Process -Id (Get-NetTCPConnection -LocalPort PORT).OwningProcess

PowerShell view all processes using port,
netstat -ano | Select-String 'PORT'

PowerShell find files on Windows
PS C:\Windows\system32> Get-ChildItem -Path C:\ -Include FILENAME -File -Recurse -ErrorAction SilentlyContinue -force

PowerShell list drives,
Get-PSDrive

PowerShell delete recovery partition
PowerShell as admin
diskpart
list disk
select disk #
list partition
select partition #

PowerShell find pattern in file
Select-String -Path C:\Windows\System32\drivers\etc\hosts -Pattern '1.2.3.4'
Select-String -Path C:\Path\To\File -Pattern 'stringToFind'

PowerShell set service to automatic,
Set-Service -Name SERVICE -StartupType Automatic

PowerShell, equivalent of for i in $(cat file.txt); do
foreach ($i in Get-Content .\file.txt) {Write-Output $i}

Linux ping count vs PowerShell ping count:
ping -c
ping -n

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment