Share via


Delete Remote Files Via WMI and Powershell

Ever wanted to delete a file on a remote computer without using the net use/shared folder stuff?  Well, with powershell, or more accurately, with WMI, you can:

PS C:\Program Files\Microsoft\> $a = Get-WMIObject -query "Select * From CIM_DataFile Where Name ='C:\\test.txt'" -computer "meng-test1"
PS C:\Program Files\Microsoft\> $a.Delete()

 What the first line says is get me all CIM_DataFile whose name is C:\test.txt on the computer whose name is meng-test1..  That returns you a CIM_DataFile object which has a method called Delete that does exactly what the name suggests.