If there is a problem finding a file, or checking for a container object,
then call for
PowerShell's Test-Path; it will respond with a 'True or False'.
Note 1: The only result that PowerShell can return is a
true or a false. However, Test-Path cries out for an 'if' statement to act
upon the output, thus:-
# PowerShell Checks If a File Exists $WantFile = "C:\Windows\explorer.exe"
$FileExists = Test-Path $WantFile If ($FileExists -eq $True)
{Write-Host "Yippee"} Else {Write-Host "No file at this location"}
Note 2: As with all these scripts, please amend
my examples to fit your environment.
Test-Path Environmental Variables
In addition to physical file locations, you can also employ Test-Path
to interrogate the registry or as here, Environmental Variables.
# PowerShell Test-Path for Environmental Variables Test-Path
env:\PathExt
Challenge: Which of these are really Environment
variables? Public, Private, Temp, Tump and Tmp
One of the under-rated jobs for Test-Path is to validate that the path
part of a script really exists.
Clear-Host # PowerShell script to check a valid path $ImageFiles
= "H:\Sports\fun_pictures\" $ValidPath = Test-Path $ImageFiles
-IsValid If ($ValidPath -eq $True) {Write-Host
"Path is OK"} Else {Write-Host "Mistake in ImageFiles variable"}
Recommended: Solarwinds' Permissions Analyzer - Free Active Directory Tool
I like the
Permissions Monitor because it enables me to see WHO has permissions
to do WHAT at a glance. When you launch this tool it analyzes a users effective NTFS
permissions for a specific file or folder, and takes into account network share
access, then displays the results in a nifty desktop dashboard!
Think of all the frustration that this free SolarWinds utility saves when you are
troubleshooting authorization problems for user's access to a resource.
Here is a real-life problem, I wanted to see if there were any files
which are not .gif, pnp, bmp or .jpg, that have got mixed up with my photo collection.
Assumption: the files I (we) are interested in are stored in
H:\Sports\fun_pictures\
# Script to check for rogue picture format. Clear-Host $ImageFiles
= "H:\Sports\fun_pictures\*.*" $RogueExists = Test-Path $ImageFiles
` -exclude *.jpg, *.gif, *.png, *.bmp
If ($RogueExists -eq $True) {Write-Host ` "Non .jpg .gif .png .bmp
images"} Else {Write-Host "There are no unknown formats."}
Note 3: You will have to edit the above file
radically if you want it to work on your computer. Change the
values for $ImageFiles and also tweak the -exclude file extensions.
# PowerShell Test-Path -Exclude Clear-Host $WantFile = "C:\Windows\.*" $FileExists
= Test-Path -Path $WantFile -Exclude *.* If ($FileExists -eq $True)
{Write-Host "Yippee"} Else {Write-Host "No file at this location"}
Note 4: I created this script deliberately.
It makes no sense in
practical terms. Please adjust the value of Exclude *.* to *.exe or
*.dll. As you can see wildcards are allowed in the path an in the
extensions.
Note 5: Here I have explicitly used the -Path
parameter, however, any value after Test-Path is assumed to be the
location, thus -Path is optional.
Note 6: I often find that experimenting with
Get-ChildItem alongside Test-Path
reveals the strenghts and weaknesses of these two cmdlets.
Guy Recommends: SolarWinds' Free Bulk Import Tool
Import users from a spreadsheet. Just provide a list of the
users with their fields in the
top row, and save as .csv file. Then launch this FREE utility and match
your fields with AD's
attributes, click to import the users. Optionally, you can
provide the name of the OU where the new accounts will be born.
There are also two bonus tools in this free download, and all 3 have been approved by Microsoft:
# Find out more about PowerShell Test-Path cmdlet Clear-Host Get-Help Test-Path
-full
Note 7: By interrogating Test-Path with Get-Help we can unearth useful
parameters such as -IsValid to check the path. There are also
-include and -exclude, which refine the contents of your search. It
was thanks to Get-Help that I discovered -pathType.
The main purpose of the -PathType parameter is to check if the object
is a file or a folder. However, it can also be used in the registry to check
for keys or data.
Trap: HKLM: needs that colon.
If you simply export areas of the registry, then copy as:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon
there are problems.
HKEY_LOCAL_MACHINE is fine, the space between Windows and NT is no
problem, but the lack of a colon is a show-stopper, it should be:
HKEY_LOCAL_MACHINE:\SOFTWARE\Microsoft\Windows
NT\CurrentVersion\Winlogon
Guy Recommends: A Free Trial of the Network Performance Monitor
(NPM)
SolarWinds'
Orion performance monitor
will help you discover what's happening on your network. This
utility will also guide you through troubleshooting; the dashboard will
indicate whether the root cause is a broken link, faulty equipment or
resource overload.
Perhaps the NPM's best feature is the way it suggests solutions to network
problems. Its
second best feature is the ability to monitor the health of individual VMware
virtual machines. If you are interested in troubleshooting, and creating network maps, then I recommend that you take advantage of SolarWinds' offer.
The classic job for PowerShell Test-Path is to check that a file exists.
However, you can extend its usefulness by testing registry paths, or to search
for files with a particular extension.
If you like this page then please share it with your friends
Please email me if you have a better script examples. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft operating systems.
Fortunately, SolarWinds
have created the
Free WMI Monitor so that you can actually see and understand these gems of
performance information. Take the guess work out of which
WMI counters to use for applications like Microsoft Active Directory,
SQL or Exchange Server.