Powershell

Set-ItemProperty (Microsoft.PowerShell.Management)

PowerShell is a dynamic command-line scripting language developed by Microsoft. It offers a wide variety of features and instructions made to handle many facets of the Windows operating system and automate administrative duties. One such functionality is the “Set-ItemProperty” cmdlet that comes under the “Microsoft.PowerShell.Management” module.

This article aims to explore the capabilities and features of the “Set-ItemProperty” cmdlet, showcasing its importance in system administration.

What is Set-ItemProperty?

The “Set-ItemProperty” is an essential cmdlet in PowerShell that enables users to modify the properties of an item or object, such as files, folders, registry keys, or even certificates. It allows for seamless manipulation of various attributes associated with these items, including permissions, attributes, security settings, and much more.

Syntax

Set-ItemProperty [-Path*] <String[]> [-Confirm] [-Credential <PSCredential>] [-Exclude <String[]>] [-Filter <String>] [-Force] [-Include <String[]>] -InputObject* <PSObject> [-PassThru] [-UseTransaction] [-WhatIf][<CommonParameters>]

 

The parameters for the above syntax are:

  • Path: Required. Add the path to the property you want to modify.
  • Confirm: Asks for confirmation before executing the command.
  • Credential: Specifies the user account that has permission to execute this action. The default is the current user.
  • Exclude: Specifies which items to exclude from the execution.
  • Filter: Adds a filter in the language of the provider.
  • Force: Forces the command to set the property on the given item that, otherwise, is inaccessible to the user.
  • Include: Adds only the items on which the execution occurs.
  • InputObject: States the object having the properties that are changed by this command.
  • PassThru: Returns the object that represents the item property. By default, it does not display any output.
  • UseTransaction: Includes the cmdlet when the transaction is active.
  • WhatIf: Shows what will happen if the cmdlet runs.

Applications of Set-ItemProperty

The “Set-ItemProperty” serves as a powerful tool for system administrators to automate administrative tasks and enforce desired system configurations across numerous items or objects. It can be used, for instance, to complete the following tasks:

 

1. Configure with the Windows Registry

One prominent use case of the “Set-ItemProperty” lies in its utilization within the Windows Registry. The Windows Registry, acting as a hierarchical database, stores crucial configuration settings for Windows and other installed applications. Through “Set-ItemProperty”, administrators can effortlessly modify registry keys and values, ensuring optimal system performance, compatibility, and security.

Consider modifying the “MaxCachedIcons” property’s value at the “HKEY_CURRENT_USER\Control Panel\Desktop” path. We can achieve this with the following code snippet:

$registryPath = "HKCU:\Control Panel\Desktop"
$property = "MaxCachedIcons"
$newValue = 2550

Set-ItemProperty -Path $registryPath -Name $property -Value $newValue</td>

 

The “HKCU: drive” notation is used in this example to describe the registry key path, which is then followed by the relative path to the required key. Next, give the name of the property that needs to be changed and assign it a new value. The appropriate registry update is handled by the “Set-ItemProperty” cmdlet.

2. Modify Other System Configurations

The “Set-ItemProperty” can also be utilized to change/modify numerous additional system configurations. For instance, by using this cmdlet, we can modify the value of a system environment variable.

Let’s look at an instance where we wish to add a new directory to the “Path” environment variable:

$envVariable = "Path"
$newValue = "$env:Path;C:\NewDirectory"
$currentValue = [Environment]::GetEnvironmentVariable($envVariable, "Machine")
$updatedValue = "$currentValue;$newValue"
[Environment]::SetEnvironmentVariable($envVariable, $updatedValue, "Machine")

 

The environment variable “($envVariable)” in this example is retrieved from the “Machine” level (system-wide variables) using the “[Environment]::GetEnvironmentVariable()” method. The new value “($newValue)” is then attached to the existing value, with a semicolon between them. The updated value is then set back to the environment variable via the “[Environment]::SetEnvironmentVariable()” function.

3. Interact with File Systems

In addition to registry manipulation, the “Set-ItemProperty” proves instrumental in managing file systems. System administrators can leverage this cmdlet to set or modify file attributes, such as Read-Only, Hidden, or Archive, to control file visibility and protection.

Moreover, the “Set-ItemProperty” enables administrators to assign appropriate file permissions, granting or revoking access rights for different users or groups, thereby enhancing the security of sensitive files and folders. Following is the demonstration:

Set-ItemProperty -Path "D:\job\external fyp doc.pdf" -Name IsReadOnly -Value $True

 

This command sets the value of the “IsReadOnly” property of the “external fyp doc.pdf” file to “True” which will restrict it to Read-only.

Conclusion

The “Set-ItemProperty” cmdlet, belonging to the “Microsoft.PowerShell.Management” module, provides a versatile and convenient way to configure with the Windows Registry, modify other System Configurations, and interact with File Systems.

About the author

Hiba Shafqat

I am a Computer Science student and a committed technical writer by choice. It is a great pleasure to share my knowledge with the world in which I have academic expertise.