Posted by: Colin Smith
Microsoft Windows, Powershell, Windows Administration
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.