Exploring Set-CimInstance in PowerShell
Welcome back to Wahman’s PowerShell Blog! Today we’re diving into one of the powerful cmdlets that let you interact with system configuration using CIM (Common Information Model): Set-CimInstance.
What is Set-CimInstance?
Set-CimInstance is a cmdlet that allows you to modify properties of an existing CIM instance on a local or remote CIM server. In simpler terms, you can use it to make changes to various system settings that are exposed through WMI/CIM classes. It calls the ModifyInstance method of a CIM class to apply those changes.
This can be useful for configuration tasks such as changing network adapter settings, updating time zones, modifying firewall configurations, and more.
Getting Started: Begin Slowly, Then Go Deep
Let’s explore four practical and progressively advanced examples of using Set-CimInstance.
Example 1 – Rename the Computer Description
Level: Beginner
Update the computer description that you see when you view system properties.
$CimInstance = Get-CimInstance -ClassName Win32_OperatingSystem
$CimInstance.Description = "My Awesome PowerShell Machine"
Set-CimInstance -InputObject $CimInstance
Example 2 – Change the Time Zone
Level: Intermediate
This example changes the system’s time zone to UTC.
$TimeZone = Get-CimInstance -ClassName Win32_TimeZone
$TimeZone.Description = "UTC"
Set-CimInstance -InputObject $TimeZone
Note: For accurate operations, you should query available time zones and assign the proper properties.
Example 3 – Disable a Network Adapter
Level: Intermediate to Advanced
Disable a specific network adapter using its name.
$adapter = Get-CimInstance -ClassName Win32_NetworkAdapter | Where-Object { $_.NetConnectionID -eq "Ethernet" }
$adapter.NetEnabled = $false
Set-CimInstance -InputObject $adapter
Important: This may disconnect you from the network, so use it carefully!
Example 4 – Change the Caption of a Logical Disk (for demo purposes)
Level: Advanced
Although some properties are read-only or require elevated permissions, let’s try modifying an editable property (even for academic purposes) of a disk.
$LogicalDisk = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DeviceID = 'C:'"
$LogicalDisk.VolumeName = "SystemDrive"
Set-CimInstance -InputObject $LogicalDisk
Note: Permissions may be required. Some changes won’t take effect until the system is refreshed or rebooted.
Conclusion
Set-CimInstance is a versatile cmdlet that opens doors for powerful system configuration through PowerShell. Always verify your WMI classes and property values before making changes, and ensure you have the necessary admin privileges when needed.
Happy scripting, and I will see you in the next post!
Leave a Reply