Show pageOld revisionsBacklinksBack to top This page is read only. You can view the source, but not change it. Ask your administrator if you think this is wrong. ====== File Operations ====== ====== Path References ====== <code powershell> $env:userprofile\Desktop $Home\Desktop ~\Desktop </code> === User Split-Path to get folder name === <code powershell> cd $(Split-Path $PROFILE) </code> ====== Common Commands By Example ====== === List Items with Filter/Search for files with matching string in name === Example of finding a file somewhere in the filesystem and outputting the full path and filename. <code powershell> Get-ChildItem -Recurse -Filter "csc.exe" | foreach {$_.Fullname} </code> Use a regex with ''-Match'' operator for more advanced filtering. <code powershell> gci -Recurse | where {$_.Name -Match "^csc.*"} | foreach {$_.Fullname} </code> === List Items === <code powershell> $exclude = @("*.log", "*.tmp", "log*.txt") $files = ls -Recurse -Exclude $exclude Compress-Archive -Path $files -DestinationPath $home\desktop\webagent.zip </code> <code powershell> $base_path = "$home\downloads" $exclude_dir_files = @("$base_path\tmp\*", "$base_path\log\*", "*.tmp", "*.log" ) ls -Recurse -Exclude $exclude_dir </code> Example of using ''-Exclude'' and additional regex filtering with ''-notmatch'' operator. This is useful for excluding folders. <code powershell> $exclude = @("*.log", "*.tmp", "log*.txt*") $files = ls -Recurse -Exclude $exclude $files = $files | where {$_.FullName -notmatch ".*\\logs"} Compress-Archive -Path $files -DestinationPath $home\desktop\webagent.zip </code> * Use the ''-File'' or ''-Directory'' option of ''Get-ChildItem'' to list on those items. <code powershell> $exclude = @("*.log", "*.tmp", "log*.txt*") $files = ls -File -Recurse -Exclude $exclude Compress-Archive -Path $files -DestinationPath $home\desktop\webagent.zip </code> === Search through files for string/grep === Example <code powershell> Get-ChildItem -Recurse *.config | Select-String "foo" -List | Select Path </code> === Create File === <code powershell> New-Item -force $env:temp\foo\foo.txt </code> === Zip Files === Example <code powershell> Compress-Archive -Path .\* -DestinationPath $env:userprofile\desktop\webapp1.zip </code> Example <code powershell> $files = ls -Path .\* -Exclude @("*.tmp", "*.log") Compress-Archive -Path $files -DestinationPath $env:userprofile\desktop\webapp1.zip </code> <code powershell> $files = ls -Recurse | Where-Object {$_.FullName -notlike "*log*"} Compress-Archive -Path $file -DestinationPath $env:userprofile\desktop\webapp1.zip </code> List contents of archive <code powershell> $archive = [System.IO.Compression.ZipFile]::OpenRead("$env:userprofile\downloads\archive.zip") $archive.Entries | Select-Object -Property FullName </code> Unzip zip archives contained in another zip archive. <code powershell> Expand-Archive -DestinationPath .\zips '.\all_reports.zip' ls .\zips\ | % {Expand-Archive -DestinationPath $env:temp $_.FullName} </code> === Create a folder with name following scheme === <code powershell> $suffix = 1 while ((Test-Path "$env:temp\mg-$suffix") -ne $false) { $suffix += 1 } mkdir "$env:temp\mg-$suffix" </code> <code powershell> $names = @("alpha", "beta", "gamma") $name = 0 while ($name -lt $names.Count) { if ((Test-Path "$env:temp\$names[$name]") -eq $false) { break } $name += 1 } if ($name -eq $names.Count) { $name = 0 } mkdir "$env:temp\$($names[$name])" </code> powershell/qnd/file_operations.txt Last modified: 2022/10/06 20:13by mgupton