Deleting a file with WMI

Following on from the last post this is how you can delete a file.

Use the same file structure as previously

This time you need to use the CIM_DataFile class. It’s one of the few classes I’ve found that doesn’t have a Win32_ equivalent.

The class has a Delete method.  Assuming you know the full path to the file

Get-CimInstance -ClassName CIM_DataFile -Filter “Name=’C:\\Expendable\\Target1\\proc.txt’ ” |
Invoke-CimMethod -MethodName Delete


You can perform most actions on files and folders using WMI but you can’t create a file or a folder

This entry was posted in File system, PowerShell and WMI, PowerShell V3. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s