Normally I would use just the Test-Path cmdlet for this. The requirement of a project that I’m on needed a “user friendly” output option as well. Below is an example of that user friendly output.
Here is the code:
Here is the code:
<# .SYNOPSIS Test to see if a file exists. .DESCRIPTION Returns TRUE if the file submitted is exists. Returns FALSE if it does not. .PARAMETER ExcelFile The name of the file being tested .EXAMPLE get-excelFile Det1.xls -DisplayInfo File found True Verifies that a file names "Det1.xls" exists and also displays the user friendly information of "File Found" to the display. The value of TRUE was returned to the pipeline. .EXAMPLE get-excelFile "demo.xls" File not found Verified that the file named "demo.xls" did not exist in the location specified. The value FALSE was returned to the pipeline. #> Function Get-File { Param ( [Parameter(Position=0,Mandatory=$True)]$File, [Switch]$DisplayInfo = $False ) If (Test-Path $File) { # Displays user friendly info on the display. If ($DisplayInfo) { Write-Host "File found" -ForegroundColor Green } Write-Output $True} Else { # Displays user friendly info on the display. If ($DisplayInfo) { Write-Host "File not found" -ForegroundColor Red } Write-Output $False }
Comments