msgbartop
Everybody get your shell on!
msgbarbottom

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

Tags:

Leave a Comment