Frequently we relocate a large number of employee home folders in bulk and we need to verify that the move was successful, or we want to test the validity of a large number of network shared folders. This utility does that utilizing the Powershell test-path commandlette.
If you want a basic understanding of how the test-path commandlette works, type this in your Powershell console window:
get-help test-path –full
Here is a general description of what this script utility, Test-Paths.ps1 does:
.SYNOPSIS
This script will test the validity of paths that are contained in the paths.ini file. Output is generated to a CSV file, and to Out-GridView.
.DESCRIPTION
The targets of the test-path command are pulled from the “paths.ini” file that is collocated with the test-paths.ps1 file.
Each target is tested using the Powershell test-path commandlette. Results are stored along with the path name in two output methods.
out-gridview and filename.csv
.PARAMETERS
-nogridview: Prevents the script from generating the Out-GridView window.
-noexport: Prevents the script from generating the exported CSV file.
-outfile filename.csv: Use an alternative name for the output file. CSV extension is best. The default if this switch is not added is testpathresult.csv.
.EXAMPLES
This will give two outputs. A file named testpathresult.csv and the out-gridview window:
.\test-paths.ps1
This example will give no out-gridview window, but will save a CSV file named patrick.csv:
.\test-paths.ps1 -nogridview -outfile patricks.csv
This example will give only the out-gridview window:
.\test-paths.ps1 –noexport
Here are a couple of examples with the script in action. In this first one I will get all failed status results for the test-path commands, but that is because I am using simulated directory paths.
Here are the contents of the paths.ini file which is collocated with the script:
Figure 1: Contents of Paths.ini File
Here are a few screen shots of the utility being run, and some of the selected output screen shots.
.\test-paths.ps1
Figure 2: Command Window Output
You can see that the Powershell command window echoes the target currently being tested.
Since the above example did not use either the of the two exclusion switches, both out-gridview and a CSV file were generated. Here are images of both types of output:
Figure 3: Out-gridview
Figure 4: Testpathresult.CSV File
Notice there are two columns in Figure 3: Accessible and HomeDirPath. In each of these the path tested was shown as False because the path was not found.
Here is another example, but this one excludes the export to the CSV file.
.\test-paths.ps1 –noexport
I added “c:windows” to the paths.ini file to show that the test-path can actually find a valid path. With this one we still see the out-gridview window, but no CSV file is generated. Notice that now we have a True in the Accessible column.
Figure 5: True Path Now Found
And finally, the last example where an alternate output file name is generated using the –outfile parameter:
.\test-paths.ps1 –outfile february28th.csv -nogridview
With this one no out-gridview window is generated, but the output file is unique and will not be overwritten the next time the utility is run.
Figure 6: Alternate Output File Naming
In summary, this utility provides an easy way to test a few, or thousands of network paths very easily.
It is run in a Windows Powershell environment. The target paths are inserted in the paths.ini text file, and the command is run as detailed above.
Let me know if you have any questions about this.
Thanks
Patrick Parkison
Below is the code used in the test-paths.ps1 script.
###################################################################################
<#
.Patrick Parkison
pp1071@att.com
.SYNOPSIS
This script will test the validity of paths that are contained in the paths.ini file. Output is generated to a CSV file, and to Out-GridView.
.DESCRIPTION
The targets of the test-path command are pulled from the “paths.ini” file that is co-located with the test-paths.ps1 file.
Each target is tested using the Powershell test-path commandlette. Results are stored along with the path name in two output methods.
out-gridview and filename.csv
.PARAMETER
-nogridview: Prevents the script from generating the Out-GridView window.
-noexport: Prevents the script from generating the exported CSV file.
-outfile: Use an alternative name for the output file. CSV extension is best. The default if this switch is not added is testpathresult.csv
.EXAMPLES
This will give two outputs. A file named testpathresult.csv and the out-gridview window:
.\test-paths.ps1
This example will give no out-gridview window, but will save a CSV file named patrick.csv:
.\test-paths.ps1 -nogridview -outfile patricks.csv
This example will give only the out-gridview window:
.\test-paths.ps1 -noexport
#>
param([switch] $noGridview, [switch] $noExport, [string]$outfile = “testpathresult.csv”)
#Change the title bar of the script window. This is helpful for long running scripts.
$Host.UI.RawUI.WindowTitle = “Running test-path utility.”
#Makes an array, or a collection to hold all the object of the same fields.
$dataColl = @()
#Get location of the script. Info will be used for getting location of all test targetrs, and for saving output to the same folder.
function Get-ScriptPath
{
Split-Path $myInvocation.ScriptName
}
#ScriptPath will be used to place the output file.
$scriptPath = get-scriptpath
#Paths.ini is a text file containing a list of targets e.g. \servernamesharename
$sourcefile = $scriptPath + “paths.ini”
<#
This is the output CSV file. It is overwritten each time the script is run.
If a historical record is desired, a date can be appended to the file name. See this reference on how to do that: https://thescriptlad.com/?s=date
#>
$outputfile = $scriptPath + “” + $outfile
foreach ($path in (gc $sourcefile)){
$dataObject = New-Object PSObject
Write-Host “Scanning: $path”
Add-Member -inputObject $dataObject -memberType NoteProperty -name “Accessible” -value (Test-Path $path )
Add-Member -inputObject $dataObject -memberType NoteProperty -name “HomeDirPath” -value $path
$dataColl += $dataObject
}
#This section is used to generate the out-gridview display.
if (!$noGridview)
{
$label = “Test-Path Results. Total Responses: ” + $dataColl.count
$dataColl | Out-GridView -Title $label
}
#Output to the CSV file for use in Excel.
if (!$noExport)
{
$dataColl | Export-Csv -noTypeInformation -path $outputfile
}
#Restore the default command window title bar.
$Host.UI.RawUI.WindowTitle = $(get-location)