powershell:qnd:file_operations

This is an old revision of the document!


File Operations

Path References

$env:userprofile\Desktop
 
$Home\Desktop
 
~\Desktop

Search through files for string/grep

Example

Get-ChildItem -Recurse *.config | Select-String "foo" -List | Select Path

Create File

New-Item -force $env:temp\foo\foo.txt

Zip Files

Example

Compress-Archive -Path .\* -DestinationPath $env:userprofile\desktop\webapp1.zip

Example

$file=Get-ChildItem -Path .\* -Exclude "*.tmp, *.log"
Compress-Archive -Path $file -DestinationPath $env:userprofile\desktop\webapp1.zip
$files = ls -Recurse | Where-Object {$_.FullName -notlike "*log*"}
Compress-Archive -Path $file -DestinationPath $env:userprofile\desktop\webapp1.zip

List contents of archive

$archive = [System.IO.Compression.ZipFile]::OpenRead("$env:userprofile\downloads\archive.zip")
 
$archive.Entries | Select-Object -Property FullName
  • powershell/qnd/file_operations.1649099758.txt.gz
  • Last modified: 2022/04/04 19:15
  • by mgupton