Skip to content

Files and folders

Terminal window
Set-Location C:\Projects # cd
Get-ChildItem # ls
Get-ChildItem -Force # include hidden files
Get-ChildItem -Recurse -Filter *.log
Get-ChildItem -File | Sort-Object LastWriteTime -Descending | Select-Object -First 10
Test-Path C:\Projects\app\.git # True / False
Terminal window
New-Item -ItemType Directory -Path .\logs
New-Item -ItemType File -Path .\notes.txt
Copy-Item .\notes.txt .\backup\
Copy-Item .\src .\src-backup -Recurse
Move-Item .\old.txt .\archive\
Rename-Item .\draft.txt final.txt
Remove-Item .\tmp -Recurse -Force

Add -WhatIf to any of these to see what would happen without doing it:

Terminal window
Remove-Item .\tmp -Recurse -WhatIf
Terminal window
Get-Content .\app.log # whole file
Get-Content .\app.log -Tail 50 # last 50 lines
Get-Content .\app.log -Tail 20 -Wait # follow it, like tail -f (Ctrl+C to stop)
Set-Content .\out.txt "first line" # overwrite
Add-Content .\out.txt "another line" # append
Terminal window
Select-String -Path .\*.log -Pattern "ERROR"
Get-ChildItem -Recurse -Filter *.java | Select-String "TODO"
Select-String -Path .\app.log -Pattern "timeout" -Context 2,2 # 2 lines before and after
Terminal window
Compress-Archive -Path .\site\* -DestinationPath .\site.zip
Expand-Archive -Path .\site.zip -DestinationPath .\site -Force
Terminal window
Get-FileHash .\installer.exe -Algorithm SHA256