Quantcast
Channel: WMI – PowerShell for Windows Admins
Viewing all articles
Browse latest Browse all 140

Deleting Folders with WMI

$
0
0

I recently saw a question about deleting folders with WMI.

Let’s create a file structure we’re happy to delete

New-Item -Path c:\ -Name Expendable -ItemType Directory
New-Item -Path c:\Expendable -Name Target1 -ItemType Directory
New-Item -Path c:\Expendable -Name Target2 -ItemType Directory

Get-Process | Out-File -FilePath c:\Expendable\Target1\proc.txt
Get-Service | Out-File -FilePath c:\Expendable\Target2\serv.txt

A look through the WMI classes brings out Win32_Directory

PS> $class = Get-CimClass -ClassName Win32_Directory
PS> $class.CimClassMethods | ft -a

Name ReturnType Parameters
—- ———- ———-
TakeOwnerShip UInt32 {}
ChangeSecurityPermissions UInt32 {Option, SecurityDescriptor}
Copy UInt32 {FileName}
Rename UInt32 {FileName}
Delete UInt32 {}
Compress UInt32 {}
Uncompress UInt32 {}
TakeOwnerShipEx UInt32 {Recursive, StartFileName, StopFileName}
ChangeSecurityPermissionsEx UInt32 {Option, Recursive, SecurityDescriptor,…}
CopyEx UInt32 {FileName, Recursive, StartFileName, StopFileName}
DeleteEx UInt32 {StartFileName, StopFileName}
CompressEx UInt32 {Recursive, StartFileName, StopFileName}
UncompressEx UInt32 {Recursive, StartFileName, StopFileName}
GetEffectivePermission Boolean {Permissions}

The Delete method looks promising

CIM uses inert objects so we need to get an instance an pipe it into Invoke-CimMethod

Get-CimInstance -ClassName Win32_Directory -Filter “Name=’C:\\Expendable’” |
Invoke-CimMethod -MethodName Delete

When you delete a folder like this – the folder, its contents, any subfolders and their contents are deleted. They don’t appear in the recycle bin either so its a one way trip


Viewing all articles
Browse latest Browse all 140

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>