(Powershell) File Delete

Posted by keith.wirch at October 16, 2013

Category: Powershell, Uncategorized

I find VBScript to be buggy and doesn’t flow very well for me.  I’m still decent at it but Powershell has been my scripting language of choice for a while.  Here is another little diddy I made out of Powershell.

I present to you…. File Delete!  Yea it’s a common thing you do I know but darn it, I don’t like using the admin share all the time to delete files off hundreds of computers.  I execute this script… kick back… watch the beauty of the text scroll up my screen and watch for any errors and investigate.  All while the user is using the computer and they never know!  Makes me feel like some sort of wizard… or ninja….

Ok, grown up time Keith.

So the script is similar to the rest.  Change the last line to your computers.txt file path.

Line 4 is then the file path you want to delete on your remote computer.  This script takes advantages of UNC paths for windows so when you enter this part in, just think of it as you would for a standard admin share.  Remember you will need access to the file location.  Powershell will use the credentials that was opened with your Powershell or Powershell ISE window.

Because I am also experimenting with using Log files, you’ll also need to make an area for the log file for this script.  Or you can delete that area.  I’m starting to like log file though in my scripts.

 function delete-remotefile {
           $file = "\\$_\c$\path\deleted\folder\or\items"
           if (test-path $file)
           echo "$_ File Exists"
           Remove-Item $file -force
           echo "$_ Files deleted"

 # Reads list and pipes to function
 Get-Content C:\Scripts\FileDelete\Computers.txt | delete-remotefile >> C:\Scripts\FileDelete\Log.txt

Leave a comment

(required) (will not be published)