Guy recommends :
Free SolarWinds
VM Console

Solarwinds VM Console Free Download

Find out which of your VMs are a waste of space and which VMs need more resources.



PowerShell Test-Path

PowerShell Test-Path

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'.

 ♣

Classic Example:  PowerShell Checks If a File Exist

Start with a simple script to check the existence of your file.

# PowerShell Check If File Exists
$WantFile = "C:\Windows\explorer.exe"
Test-Path $WantFile

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

PowerShell Test-Path -IsValid

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 ToolFree Permissions Analyzer for Active Directory

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.

Download SolarWinds' Free Permissions Analyser - Active Directory Tool

PowerShell Test-Path -Exclude

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.

A Test-Path Example To Make You Think

# 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 ToolFree Download of Solarwinds  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:

  1. Bulk-import new users into Active Directory.
  2. Seek and zap unwanted user accounts.
  3. Find inactive computers.

Download your FREE bulk import tool.

Research The PowerShell Test-Path Cmdlet

# 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.

Test-Path -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.

Clear-Host
$Location = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon'
Test-Path $Location -PathType container

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)Review of Orion NPM v10

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.

Download a free trial of the Network Performance Monitor.

The Rest of the Path Family

Here is a simple technique to find more cmdlets.  Get-Command -noun often throws up interesting results, for example Resolve-Path.

# PowerShell Path Cmdlet Research
Clear-Host
Get-Command -noun Path

Convert-Path
Join-Path
Resolve-Path
Split-Path
Test-Path

See more real-life tasks for PowerShell »

Summary of PowerShell Test-Path

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

 


See more Microsoft PowerShell tutorials:

PowerShell Home   • Test-ServerHealth  • Test-SystemHealth   • Test-Connection  • Test-Path

PowerShell Logon Script  • PowerShell add printer  • PowerShell Schedule Task

Map Network Drive  • Exchange 2010 PowerShell Cmdlets   • Exchange 2010 Performance Monitor

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.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Custom Search

Site Home

Guy Recommends: WMI Monitor and It's Free!Solarwinds WMI Monitor

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.

Download your free copy of WMI Monitor

Author: Guy Thomas Copyright © 1999-2012 Computer Performance LTD All rights reserved.

Please report a broken link, or an error to: