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
- 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.
- Open a command prompt window and navigate to the directory where you saved the file.
- Type cscript filename.vbs at the command prompt.
- 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).
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](https://msdn.microsoft.com/library/aa387236) class. Ensure that you pass the entire path name when calling the [Rename](https://msdn.microsoft.com/library/aa393051) 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.
|...determine whether users have .MP3 files stored on their computer?||
Use the [CIM_DataFile](https://msdn.microsoft.com/library/aa387236) class and select files using the following [WQL](querying-with-wql.md) WHERE clause: Where Extension = "MP3".
|...create shared folders on a computer?||
Use the [Win32_Share](https://msdn.microsoft.com/library/aa394435) class and the [Create](https://msdn.microsoft.com/library/aa389393) method.
|...copy a folder?||
Use the [Win32_Directory](https://msdn.microsoft.com/library/aa394130) class and the [Copy](https://msdn.microsoft.com/library/aa389347) method. For PowerShell, you can simply use the Copy-Item cmdlet.
|...move a folder?||
Use the [Win32_Directory](https://msdn.microsoft.com/library/aa394130) class and the [Rename](https://msdn.microsoft.com/library/aa393057) method. For PowerShell, you can simply use the Move-Item cmdlet.