Get All Files with a Certain Extension
# add '-recurse' to include sub folders Get-ChildItem -Path $directory -Filter "*.CSV"
Get All Folders in a Directory
# add '-recurse' to include sub folders Get-ChildItem -Path $directory -Directory
Get All Files in a Directory
# add '-recurse' to include sub folders Get-ChildItem -Path $directory -File
Get All Files and Folders in a Directory
# add '-recurse' to include sub folders Get-ChildItem -Path $directory
Delete Files Older Than Specified Number of Days
# Set number of days, in the past, to delete files $days = 7 # Set directory to create logs in $directory = "C:\Test\Logs" # Get all files in the directory include '-recurse' to also get sub folders $files = Get-ChildItem -Path $directory -File # filter to ones last written to over X number of days $ToDelete = $files |...
Create Test Log Files
# Set number of days, in the past, to create log files for $days = 14 # Set directory to create logs in $directory = "C:\Test\Logs" $i=1 While($i -lt $days){ # Get Date and create log file $date = (Get-Date).AddDays(-$i) $logFile = "u_ex" + ($date.Year).ToString().Substring(2,2) + (($date.Month).ToString().PadLeft(2)).Replace(" ","0") + (($date.Day).ToString().PadLeft(2)).Replace(" ","0") + ".log" $logPath = join-path $directory $logFile $date |...
Get User’s Desktop Path
$ShellFolders = Get-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders' $ShellFolders.Desktop
Zip All Files in Folder
# Get the files to zip $FilesToZip = Get-ChildItem -Path $FolderPath -File # Load the assembly to get the zip functionality [Reflection.Assembly]::LoadWithPartialName( "System.IO.Compression.FileSystem" ) | Out-Null # Set compression level $compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal # Set Zip file pathc based on folder $ZipPath = Join-Path $FolderPath "$(Split-Path $FolderPath -Leaf).zip" # initialize the zip file $Archive = [System.IO.Compression.ZipFile]::Open( $ZipPath, "Update" ) #...
Get Folder Path of the Current Script
Function Get-ScriptDirectory{ $Invocation = (Get-Variable MyInvocation -Scope 1).Value; if($Invocation.PSScriptRoot){ $Invocation.PSScriptRoot; } elseif($Invocation.MyCommand.Path){ Split-Path $Invocation.MyCommand.Path } elseif($Invocation.InvocationName.LastIndexOf("\") -gt 0){ $Invocation.InvocationName.Substring(0,$Invocation.InvocationName.LastIndexOf("\")); } else { Get-PSDrive | Where-Object{$_.Provider.Name -eq 'FileSystem'} | Foreach-Object { Join-Path -Path $_.Root -ChildPath $_.CurrentLocation } } }