The Multifunctioning DBA

Jun 30 2009   3:28PM GMT

Test a path in Powershell

Colin Smith Colin Smith Profile: Colin Smith

this is a simple thing but it can be very useful. I use this is many of my scripts. Sometimes I want to make sure that a file is available for me to append and if not then I would like to create a new one. Take a log file for example, if the file has been moved or deleted, perhaps it grew very large and needed to be removed, the script can just create a new one without running into any problems. If the file is still available though, then the script can just append the current file. here is an example:

if (!(test-path “H:\path\file.txt”))
New-Item -type File “H:\path\file.txt”

This will check if the file exists and if not then it will create it.

 Comment on this Post

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: