WMI Tasks: Files and Folders

WMI tasks for files and folders change file or folder properties through WMI, including creating a share or renaming a file. If you want to copy a file or read and write a file, the easiest way is to use the Windows Script Host FileSystemObject rather than WMI. For other examples, see the Files and Folders section of the TechNet ScriptCenter.

CIM_DataFile is one of the few CIM classes in WMI that is implemented. Avoid enumerating or querying for all instances of CIM_DataFile on a computer because the volume of data is likely to either affect performance or cause the computer to stop responding.

The script examples shown in this topic obtain data only from the local computer. For more information about how to use the script to obtain data from remote computers, see Connecting to WMI on a Remote Computer.

The following procedure describes how to run a script.

To run a script

  1. Copy the code and save it in a file with a .vbs extension, such as filename.vbs. Ensure that your text editor does not add a .txt extension to the file.
  2. Open a command prompt window and navigate to the directory where you saved the file.
  3. Type cscript filename.vbs at the command prompt.
  4. If you cannot access an event log, check to see if you are running from an Elevated command prompt. Some Event Log, such as the Security Event Log, may be protected by User Access Controls (UAC).

Note

By default, cscript displays the output of a script in the command prompt window. Because WMI scripts can produce large amounts of output, you might want to redirect the output to a file. Type cscript filename.vbs > outfile.txt at the command prompt to redirect the output of the filename.vbs script to outfile.txt.

The following table lists script examples that can be used to obtain various types of data from the local computer.

How do I... WMI classes or methods
...rename a file without getting an error message? Use the CIM_DataFile class. Ensure that you pass the entire path name when calling the Rename method, for example, "C:\Scripts\Test.txt" instead of "Text.txt". For PowerShell, using CIM_DataFile may be inefficient. As such, you may simply use the Rename-Item cmdlet.
VB
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "!\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery ("Select * from CIM_DataFile where Name = " & "'c:\\scripts\\toggle_service.vbs'")
For Each objFile in colFiles
    errResult = objFile.Rename("c:\scripts\toggle_service.old")
Next
PowerShell
rename-item c:\scripts\toggle_service.vbs toggle_service.old
...determine whether users have .MP3 files stored on their computer?

Use the CIM_DataFile class and select files using the following WQL WHERE clause: Where Extension = "MP3".

VB
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery("Select * from CIM_DataFile where Extension = 'mp3'")
For Each objFile in colFiles
    Wscript.Echo "File Name: " & objFile.Name & "." & objFile.Extension
    Wscript.Echo "Path: " & objFile.Path
Next
PowerShell
Get-WmiObject -Class CIM_DataFile -namespace "root\cimv2" -Filter "Extension = 'mp3'" | `
   format-list Name, Extension, Path
...create shared folders on a computer?

Use the Win32_Share class and the Create method.

VB
Const FILE_SHARE = 0
Const MAXIMUM_CONNECTIONS = 25
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objNewShare = objWMIService.Get("Win32_Share")
errReturn = objNewShare.Create("C:\Finance", "FinanceShare", FILE_SHARE, MAXIMUM_CONNECTIONS, "Public share for the Finance group.")

PowerShell
$FILE_SHARE = 0
$MAXIMUM_CONNECTIONS = 25

$NewDir = new-item C:\Finance -type directory $Shares= [WMICLASS]"Win32_Share" [void]$Shares.Create("C:\Finance","FinanceShare", $FILE_SHARE, $MAXIMUM_CONNECTIONS, "Public share for the Finance group.")

...copy a folder?

Use the Win32_Directory class and the Copy method. For PowerShell, you can simply use the Copy-Item cmdlet.

VB
strComputer = "." 
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
 
Set colFolders = objWMIService.ExecQuery("Select * from Win32_Directory where Name = 'c:\\Scripts'") 
 
For Each objFolder in colFolders 
    errResults  = objFolder.Copy("D:\Archive") 
Next 
PowerShell
Copy-Item C:\Scripts -Destination D:\Archive -Recurse
...move a folder?

Use the Win32_Directory class and the Rename method. For PowerShell, you can simply use the Move-Item cmdlet.

VB
strComputer = "." 
Set objWMIService = GetObject("winmgmts:" _ 
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
 
Set colFolders = objWMIService.ExecQuery _ 
    ("Select * from Win32_Directory where name = 'c:\\Scripts'") 
 
For Each objFolder in colFolders 
    errResults = objFolder.Rename("C:\Admins\Documents\Archive\VBScript") 
Next
PowerShell
move-item -path C:\Scripts -destination C:\Admins\Documents\Archive\PowerShell

WMI Tasks for Scripts and Applications

WMI C++ Application Examples

TechNet ScriptCenter

`