Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 38. Creating and Running a Power... > Testing for the Existence of a File

Testing for the Existence of a File

You can use the following script snippets to test for the existence of a file and either delete or rename the file depending on which code snippet you choose. This has multiple uses. For example, if you want to create a listing of computers in the domain, but you don’t want the new listing appended to the current file, you can either delete or rename the existing file. Similarly, if you have a script that writes data to a file for logging, you can use one of these scripts to handle existing files.

Script LinesComments
Delete a file if it exists.
If (test-path c:\data\
computerlist.txt)
{
  remove-item c:\data\
computerlist.txt
}

The test-path cmdlet checks for the existence of the file. If it exists, it returns true and the lines within the curly brackets execute. The example checks for C:\data\computerlist.txt and deletes if it exists with the remove-item cmdlet.
Rename a file if it exists.
If (test-path c:\data\
computerlist.txt)
{
  $dt = (get-date).
tostring('MMM_dd_yyyy_hh_mm')
  $newname = "computerlist_" +
$dt + ".txt"
  rename-item -path c:\data\
computerlist.txt -newname
$newname
}

Alternatively, you can archive the item by renaming it with the rename-item cmdlet.

Note

Use this code instead of the remove-item code in the preview row if you want to archive the item.

This code renames the file by appending the date and time to the file name.
Copy a file if it doesn’t exist.
If (test-path c:\gpo_adm\
grouppolicy-server.admx)
{
#do nothing
}
else
{
  $objnet = $(new-object -com
wscript.network)
  $objnet.mapnetworkdrive
("x:", "\\dc1\admx")
  copy-item
x:\grouppolicy-server.admx
-destination
c:\gpo_adm
  $objnet.
removenetworkdrive("x:")
}

This code checks for the existence of a file and copies it from a share if it doesn’t exist.

If the file exists, the comment #do nothing is ignored. If the file doesn’t exist, it’s copied from a network share.

Because the copy-item cmdlet can’t copy from a share directly, the share must first be created using the new-object cmdlet from the wscript.network object and the mapnetworkdrive method. The network object is stored in the $objnet variable.

Note

You cannot have any spaces between the mapnetworkdrive method and the first parentheses.

With the drive mapped, the copy-item can copy the file from the mapped drive to the local destination.

Last, the removenetworkdrive method is used to remove the x: mapped network drive.

Note

You cannot have any spaces between the removenetworkdrive method and the first parentheses.



  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint