Поделиться через


Performing management tasks using CIM Cmdlets [4] – Files and Folders

 

As a part of the ongoing blog series Performing Management tasks using CIM Cmdlets we are sharing PowerShell snippets for a few computer management tasks.

In this post, we will be going over the PowerShell snippets for file and folder management. The corresponding Visual Basic samples are in the MSDN article, WMI Tasks for Files and Folders.

  • Rename a file:

PS:> $fileName = 'C:\\Temp\\Test1.txt'

PS:> $newFileName = 'C:\\Temp\\Test1_NewName.txt'

PS:> $query = "SELECT * FROM CIM_DataFile WHERE Name = '$fileName'"

PS:> # Get Instance of CIM_DataFile where file name is C:\\Temp\\Test1.txt

PS:> $file = Get-CimInstance -Query $query -Namespace root/cimv2

PS:> # Change name of file Test1.txt to Test1_NewName.txt

PS:> Invoke-CimMethod -InputObject $file -MethodName 'Rename' –Arguments @{FileName=$newFileName}

ReturnValue PSComputerName ----------- -------------- 0

 

  • Determine whether users have .MP3 files stored on their computer:

PS:> $query = "SELECT * FROM CIM_DataFile WHERE Extension = 'mp3'"

PS:> # Get Instances of CIM_DataFile with Extension property set to mp3

PS:> $mp3Files = Get-CimInstance -Query $query -Namespace root/cimv2

PS:> $mp3Files | Select Name

Name ---- c:\temp\test1.mp3 c:\temp\test2.mp3

 

  • Create shared folders on a computer:

PS:> # Create new share under C:\Temp called TestShare

PS:> Invoke-CimMethod -ClassName Win32_Share -Namespace root/cimv2 –MethodName 'Create' -Arguments @{Path='C:\Temp'; Name='TestShare'; Type=[uint32]'0'; MaximumAllowed=[uint32]'25'; Description='Public share for team'}

ReturnValue PSComputerName ----------- -------------- 0

 

We will be covering other computer management scenarios in our future posts. If you have any questions, please feel free to send them to us, or post them in the Comments section.

 

The complete list of tasks we are covering in this series is in the MSDN article, WMI Tasks for Scripts and Applications.

Links to previous posts:

Processes

Computer Hardware

Operating System

 

Thanks,

Milena Natanov [MSFT]

Standards-Based Management

Comments

  • Anonymous
    May 14, 2014
    Would it be possible to provide and example as to how to set access on a share. for example set the the share access for the everyone group to full control?

  • Anonymous
    September 16, 2014
    Some basic samples can be found on MSDN:

  1. Overview of Win32_Share class: msdn.microsoft.com/.../aa394435(v=vs.85).aspx
  2. This article talks about SetShareInfo method, but has no samples in PowerShell: msdn.microsoft.com/.../aa393598(v=vs.85).aspx We'll address this ask in future. Thanks, Milena