April 13th, 2015

PowerTip: Use PowerShell to Find File or Folder

Doctor Scripto
Scripter

Summary: Learn how to use Windows PowerShell to find a file or a folder.

Hey, Scripting Guy! Question How can I use Windows PowerShell to determine if a variable contains a path that leads to a file or folder?

Hey, Scripting Guy! Answer Use the Test-Path cmdlet to test the path stored in the variable, and then specify the PathType parameter,
           for example:

PS C:\> $fso = "c:\fso"

PS C:\> $file = "C:\fso\backup.ps1"

PS C:\> Test-Path $fso -PathType leaf

False

PS C:\> Test-Path $file -PathType leaf

True

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

0 comments

Discussion are closed.