get updates!

welcome!

Welcome to the 0direction blog! My name is Cory and I am a 10 year veteran of the IT industry currently working for MaximumASP as a Senior System Engineer specializing in Storage and Disaster Recovery. If you find something here that helps you, please leave a comment! If you see something that could be improved or have a question, please send me some feedback! Happy scripting! -Cory

Verifying that a file exists with PowerShell

If you use PowerShell a lot, there are going to be times that you need to import data from another file (like a list of servers from a text file to automate a task on many machines). This snippet sets a variable to a file path, tests the files location using the test-path function, and returns True if it exists and False if it does not.

If the file exists:

1
2
3
$file = "m:\scripts\test.txt"
$test = test-path $file
$test
True

If the file doesnt exist:

1
2
3
$file = "m:\scripts\test2.txt"
$test = test-path $file
$test
False
  • Twitter
  • Facebook
  • LinkedIn
  • Digg
  • Google Bookmarks
  • Slashdot
  • StumbleUpon
  • Live
  • FriendFeed
  • Ping.fm
  • Print
  • email
  • PDF
  • RSS

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">