Renaming a file or folder on remote machine through WMI

Welcome Forums General PowerShell Q&A Renaming a file or folder on remote machine through WMI

This topic contains 6 replies, has 4 voices, and was last updated by

10 months ago.

  • Author
  • #99318

    Points: 0
    Rank: Member


    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.

  • #99321

    Points: 398
    Helping Hand
    Rank: Contributor

    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. 🙂

    • #99324

      Points: 0
      Rank: Member

      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.

  • #99408

    Points: 536
    Helping Hand
    Rank: Major Contributor

    .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. 🙂 '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)
    • #99463

      Points: 0
      Rank: Member

      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
  • #99469

    Points: 0
    Rank: Member

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

  • #99472

    Points: 0
    Rank: Member

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

    Thank you!! Nick..have a good day.

The topic ‘Renaming a file or folder on remote machine through WMI’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort