Set-WmiInstance – Creating or Updating WMI Instances with PowerShell
Welcome back to Wahmans PowerShell Blog! Today we’re diving into Set-WmiInstance, a powerful cmdlet used to create or update an instance of an existing Windows Management Instrumentation (WMI) class. With it, you’re essentially tapping into the configuration core of Windows to automate setting system values, modifying configurations, or even creating new manageable entities!
The official Microsoft definition says:
Set-WmiInstance: “Creates or updates an instance of an existing Windows Management Instrumentation (WMI) class.”
Let’s explore 4 real-world use cases of increasing complexity for Set-WmiInstance, from beginner to more advanced examples.
✅ Example 1: Create a New File Share (Beginner)
This script creates a new SMB file share on a folder using WMI:
$share = Set-WmiInstance -Class Win32_Share -Arguments @{
Path = 'C:\MyFolder';
Name = 'MyShare';
Type = 0
}
Here we’re simply sharing an existing folder using WMI. Type 0 corresponds to Disk Drive.
✅ Example 2: Set System Time (Intermediate)
You can use WMI to set the system time (requires admin privileges):
$dt = (Get-Date).AddMinutes(1)
$timeSet = Set-WmiInstance -Class Win32_OperatingSystem -Arguments @{
LocalDateTime = $dt.ToString("yyyyMMddHHmmss.000000+000")
} -EnableAllPrivileges
Note: Setting the system clock like this is rarely necessary, but demonstrates modifying system settings via WMI.
✅ Example 3: Create a New Environment Variable (Intermediate)
You can use Set-WmiInstance to create a per-machine environment variable:
Set-WmiInstance -Class Win32_Environment -Arguments @{
Name = 'MyEnvVar';
VariableValue = 'MyValue';
UserName = <SYSTEM>
}
This adds a system-wide environment variable. Note that you need to use angle brackets around SYSTEM (i.e., <SYSTEM>).
✅ Example 4: Update TCP/IP Network Settings (Advanced)
This advanced example sets a static IP address using WMI. Be cautious when modifying network settings!
$adapter = Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object { $_.IPEnabled }
$adapter.EnableStatic('192.168.1.100', '255.255.255.0')
$adapter.SetGateways('192.168.1.1')
This doesn’t directly use Set-WmiInstance, but extends your understanding by showing WMI method invocation. In more complex uses, you could update these values with Set-WmiInstance by modifying instances directly.
Recap
Set-WmiInstance is your go-to cmdlet when you want to configure or create system-level elements through WMI. From file shares to environment variables and network configurations, it’s a foundational tool every PowerShell scripter should have in their arsenal.
Happy scripting, and I will see you in the next post!
Leave a Reply