Renaming a file or folder on remote machine through WMI


I am stuck in a problem for few days now, i need to create a script to rename a file or folder on remote machine in my environment. The problem, i cannot use psremoting as the service and ports required are blocked and cannot, i cannot use CIM_instance as well as the service is disabled or psexec tool. The only way i can reach remote machine is by WMI_Object method. I can get to the folder on remote machine but can’t rename or delete it. The script i am using to get to the folder is below:-

Get-WmiObject -Class win32_directory -namespace “root\cimv2” -ComputerName “remote machine” -Impersonation 3 | Where {$ -eq “C:\windows\SoftwareDistribution”}

But i do not know how to rename this “software distribution” or delete it.

I tried rename and delete method with this but did not work.

Maybe i have create a new custom object but i do not know how to do that.

Also, i want to do this task only by wmi object method.

Could someone please help me?

Ajay Kumar.

I believe if you’re trying to invoke methods on WMI classes you need to use the static WMI methods, with syntax like the following:


You may, however, have to google for the specifics of how it’s done with that specific class and method. I’m not too familiar with it. :slight_smile:

Thanks! for your suggestion Joel.

Yes, i came across this syntax but i am not able to find a way to use rename or delete method with this syntax.

.delete() and .rename(‘filename’) are instance methods (there’s no static qualifier). Usually only the create type methods are static. This worked for me locally at least. Have to double the backslash if you use a wql (sql) filter. Or you could pipe to where, but it’s much slower, since it’s returning every directory. I just took a Pluralsight class on using wmi and cim in powershell. :slight_smile: ‘root\cimv2’ is the default namespace, so you don’t have to specify it.

get-cimclass win32_directory | select -expand cimclassmethods

Name                        ReturnType Parameters                                         Qualifiers
----                        ---------- ----------                                         ----------
Rename                          UInt32 {FileName}                                         {Schema, ValueMap}
Delete                          UInt32 {}                                                 {Schema, ValueMap}

$obj = Get-WmiObject win32_directory -Filter "name = 'c:\\foo'"

# or (need full path)

Thanks! for your reply JS,

But the query is not working, i used below command and it gave me an error:

PS C:\WINDOWS\system32> $obj = Get-WmiObject win32_directory -Filter "name = 'c:\\windows\softwaredistribution'"

Get-WmiObject : Invalid query "select * from win32_directory where name = 'c:\\windows\softwaredistribution'"
At line:1 char:8
+ $obj = Get-WmiObject win32_directory -Filter "name = 'c:\\windows\sof ...
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-WmiObject], ManagementException
    + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

Look at path string. The backslash needs to be doubled up when used :slight_smile:

Yes!!! It worked. I am sorry, i did not realize i had to use double \ everywhere.

Thank you!! Nick…have a good day.