PowerShell – Find a file

| | |

If you run into a need to find a file on your computer/server this script should help make things a bit easier

Get-ChildItem -Path f:\ -Filter 2019-10-07.log -Recurse -ErrorAction SilentlyContinue -Force
Function Format-FileSize() {
    Param ([int]$size)
    If ($size -gt 1TB) { [string]::Format("{0:0.00} TB", $size / 1TB) }
    ElseIf ($size -gt 1GB) { [string]::Format("{0:0.00} GB", $size / 1GB) }
    ElseIf ($size -gt 1MB) { [string]::Format("{0:0.00} MB", $size / 1MB) }
    ElseIf ($size -gt 1KB) { [string]::Format("{0:0.00} kB", $size / 1KB) }
    ElseIf ($size -gt 0) { [string]::Format("{0:0.00} B", $size) }
    Else { "" }
}

$PathToSearch = "F:\"
$FileToSearch = "2019-10-07.log"
Get-ChildItem -Path $PathToSearch -Filter $FileToSearch -Recurse -ErrorAction SilentlyContinue -Force |
    Format-Table -AutoSize `
        @{expression = { $_.Name }; label = "File Name"; },
        @{Expression = { (Format-FileSize $_.Length) }; Label = "File Size" },
        @{expression = { $_.FullName }; label = "File Path"; },
        @{expression = { $_.LastWriteTime }; label = "Last Modified"; }
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.