Files and folders
Navigate and list
Section titled “Navigate and list”Set-Location C:\Projects # cdGet-ChildItem # lsGet-ChildItem -Force # include hidden filesGet-ChildItem -Recurse -Filter *.logGet-ChildItem -File | Sort-Object LastWriteTime -Descending | Select-Object -First 10Test-Path C:\Projects\app\.git # True / FalseCreate, copy, move, delete
Section titled “Create, copy, move, delete”New-Item -ItemType Directory -Path .\logsNew-Item -ItemType File -Path .\notes.txtCopy-Item .\notes.txt .\backup\Copy-Item .\src .\src-backup -RecurseMove-Item .\old.txt .\archive\Rename-Item .\draft.txt final.txtRemove-Item .\tmp -Recurse -ForceAdd -WhatIf to any of these to see what would happen without doing it:
Remove-Item .\tmp -Recurse -WhatIfRead and write
Section titled “Read and write”Get-Content .\app.log # whole fileGet-Content .\app.log -Tail 50 # last 50 linesGet-Content .\app.log -Tail 20 -Wait # follow it, like tail -f (Ctrl+C to stop)Set-Content .\out.txt "first line" # overwriteAdd-Content .\out.txt "another line" # appendSearch inside files
Section titled “Search inside files”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 afterCompress and extract
Section titled “Compress and extract”Compress-Archive -Path .\site\* -DestinationPath .\site.zipExpand-Archive -Path .\site.zip -DestinationPath .\site -ForceChecksums
Section titled “Checksums”Get-FileHash .\installer.exe -Algorithm SHA256