Translate
From cmd to PowerShell
Moving over from the Command Prompt? Many old commands still work as aliases, but the PowerShell versions give you objects you can sort, filter, and export. Also see From bash.
| cmd.exe | PowerShell | Learn more |
|---|---|---|
echo Another line>> notes.txt |
Add-Content -Path .\notes.txt -Value 'Another line' |
Add-Content |
cd docs |
Set-Location -Path .\docs |
Set-Location |
certutil -hashfile installer.exe SHA256 |
Get-FileHash -Path .\installer.exe -Algorithm SHA256 |
Get-FileHash |
if exist docs echo yes |
Test-Path -Path .\docs |
Test-Path |
cls |
Clear-Host |
Clear-Host |
fc a.txt b.txt |
Compare-Object (Get-Content -Path .\a.txt) (Get-Content -Path .\b.txt) |
Compare-Object |
copy notes.txt backup\ |
Copy-Item -Path .\notes.txt -Destination .\backup\notes.txt |
Copy-Item |
dir /b | clip |
Get-ChildItem -Name | Set-Clipboard |
Set-Clipboard |
mkdir docs |
New-Item -Path .\docs -ItemType Directory |
New-Item |
rmdir /s /q temp |
Remove-Item -Path .\temp -Recurse -Force -WhatIf |
Remove-Item |
curl.exe -O https://example.com/file.zip |
Invoke-WebRequest -Uri 'https://example.com/file.zip' -OutFile .\file.zip |
Invoke-WebRequest |
where git |
Get-Command git |
Get-Command |
netstat -ano | findstr :8080 |
Get-NetTCPConnection -LocalPort 8080 | Select-Object LocalAddress, LocalPort, State, OwningProcess |
Get-NetTCPConnection |
echo %date% %time% |
Get-Date -Format 'yyyy-MM-dd HH:mm:ss' |
Get-Date |
dir |
Get-ChildItem -Force |
Get-ChildItem |
ipconfig |
Get-NetIPAddress -AddressFamily IPv4 | Select-Object InterfaceAlias, IPAddress |
Get-NetIPAddress |
net user |
Get-LocalUser | Select-Object Name, Enabled, LastLogon |
Get-LocalUser |
tasklist |
Get-Process | Sort-Object CPU -Descending | Select-Object -First 10 |
Get-Process |
sc query |
Get-Service | Where-Object Status -eq 'Running' |
Get-Service |
nslookup example.com |
Resolve-DnsName -Name example.com |
Resolve-DnsName |
move notes.txt archive\ |
Move-Item -Path .\notes.txt -Destination .\archive\notes.txt |
Move-Item |
start notes.txt |
Start-Process -FilePath .\notes.txt |
Start-Process |
echo Hello> notes.txt |
Set-Content -Path .\notes.txt -Value 'Hello' |
Set-Content |
timeout /t 5 |
Start-Sleep -Seconds 5 |
Start-Sleep |
ping -n 2 1.1.1.1 |
Test-Connection -ComputerName 1.1.1.1 -Count 2 |
Test-Connection |
echo Backup finished |
Write-Output 'Backup finished' |
Write-Output |
wmic os get caption,version,lastbootuptime |
Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object Caption, Version, LastBootUpTime |
Get-CimInstance |
type notes.txt |
Get-Content -Path .\notes.txt |
Get-Content |
help dir |
Get-Help Get-ChildItem -Examples |
Get-Help |
ren draft.txt final.txt |
Rename-Item -Path .\draft.txt -NewName final.txt |
Rename-Item |
net stop Spooler && net start Spooler |
Restart-Service -Name Spooler -WhatIf |
Restart-Service |
shutdown /r /t 0 |
Restart-Computer -WhatIf |
Restart-Computer |
for %f in (*) do @echo %f |
Get-ChildItem -File | ForEach-Object { $_.Name } |
ForEach-Object |
findstr /s /n TODO docs\*.md |
Get-ChildItem -Path .\docs -Recurse -File | Select-String -Pattern 'TODO' |
Select-String |
doskey /history |
Get-History -Count 20 |
Get-History |
icacls docs |
(Get-Acl -Path .\docs).Access | Select-Object IdentityReference, FileSystemRights, AccessControlType |
Get-Acl |
dir > listing.txt |
Get-ChildItem | Out-File -FilePath .\listing.txt |
Out-File |
cd |
Get-Location |
Get-Location |
net start Spooler |
Start-Service -Name Spooler |
Start-Service |
taskkill /PID 1234 |
Stop-Process -Id 1234 -WhatIf |
Stop-Process |
net stop Spooler |
Stop-Service -Name Spooler -WhatIf |
Stop-Service |
systeminfo |
Get-ComputerInfo | Select-Object CsName, WindowsVersion, OsArchitecture |
Get-ComputerInfo |
tar -x -f docs.zip -C docs-unzipped |
Expand-Archive -Path .\docs.zip -DestinationPath .\docs-unzipped |
Expand-Archive |
tar -a -c -f docs.zip docs |
Compress-Archive -Path .\docs -DestinationPath .\docs.zip |
Compress-Archive |
Gotchas for cmd users
- Switches use a dash, not a slash.
dir /sbecomesGet-ChildItem -Recurse.dirstill works as an alias, but/sdoes not. - Variables:
%USERPROFILE%becomes$env:USERPROFILE. Set one with$env:NAME = 'value'; it lasts until you close the window. - Run a program in the current folder with
.\, for example.\setup.exe. PowerShell does not look in the current folder on its own. - Paths with spaces need quotes and
&to run:& 'C:\Program Files\App\app.exe'. - Old tools still work.
ipconfig,ping,robocopy, andnetstatrun fine from PowerShell. They return text instead of objects. - Batch files (
.bat,.cmd) still run, but scripts (.ps1) follow the execution policy.