powershell:qnd:file_operations

This is an old revision of the document!


File Operations

Path References

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

Common Commands By Example

List Items

$exclude = @("*.log", "*.tmp", "log*.txt")
$files = ls -Recurse -Exclude $exclude
Compress-Archive -Path $files -DestinationPath $home\desktop\webagent.zip
$base_path = "$home\downloads"
$exclude_dir_files = @("$base_path\tmp\*",
  "$base_path\log\*",
  "*.tmp",
  "*.log"
)
 
ls -Recurse -Exclude $exclude_dir

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

$files = ls -Path .\* -Exclude @("*.tmp", "*.log")
Compress-Archive -Path $files -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.1649271712.txt.gz
  • Last modified: 2022/04/06 19:01
  • by mgupton