Set-Item

Changes the value of an item to the value specified in the command.

语法

Path (默认值)

Set-Item
    [-Path] <String[]>
    [[-Value] <Object>]
    [-Force]
    [-PassThru]
    [-Filter <String>]
    [-Include <String[]>]
    [-Exclude <String[]>]
    [-Credential <PSCredential>]
    [-WhatIf]
    [-Confirm]
    [-UseTransaction]
    [<CommonParameters>]

LiteralPath

Set-Item
    [[-Value] <Object>]
    -LiteralPath <String[]>
    [-Force]
    [-PassThru]
    [-Filter <String>]
    [-Include <String[]>]
    [-Exclude <String[]>]
    [-Credential <PSCredential>]
    [-WhatIf]
    [-Confirm]
    [-UseTransaction]
    [<CommonParameters>]

说明

The Set-Item cmdlet changes the value of an item, such as a variable or registry key, to the value specified in the command.

示例

Example 1: Create an alias

This command creates an alias of np for Notepad.

Set-Item -Path alias:np -Value "c:\windows\notepad.exe"

Example 2: Change the value of an environment variable

This command changes the value of the UserRole environment variable to Administrator.

Set-Item -Path env:UserRole -Value "Administrator"

Example 3: Modify your prompt function

This command changes the prompt function so that it displays the time before the path.

Set-Item -Path function:prompt -Value {'PS '+ $(Get-Date -Format t) + " " + $(Get-Location) + '> '}

Example 4: Set options for your prompt function

This command sets the AllScope and ReadOnly options for the prompt function. This command uses the Options dynamic parameter of Set-Item. The Options parameter is available in Set-Item only when you use it with the Alias or Function provider.

Set-Item -Path function:prompt -Options "AllScope,ReadOnly"

参数

-Confirm

Prompts you for confirmation before running the cmdlet.

参数属性

类型:SwitchParameter
默认值:False
支持通配符:False
不显示:False
别名:cf

参数集

(All)
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

-Credential

Note

This parameter is not supported by any providers installed with PowerShell. To impersonate another user, or elevate your credentials when running this cmdlet, use Invoke-Command.

参数属性

类型:PSCredential
默认值:Current user
支持通配符:False
不显示:False

参数集

(All)
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):True
来自剩余参数的值:False

-Exclude

Specifies, as a string array, an item or items that this cmdlet excludes in the operation. The value of this parameter qualifies the Path parameter. Enter a path element or pattern, such as *.txt. Wildcard characters are permitted. The Exclude parameter is effective only when the command includes the contents of an item, such as C:\Windows\*, where the wildcard character specifies the contents of the C:\Windows directory.

参数属性

类型:

String[]

默认值:None
支持通配符:True
不显示:False

参数集

(All)
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

-Filter

Specifies a filter to qualify the Path parameter. The FileSystem provider is the only installed PowerShell provider that supports the use of filters. You can find the syntax for the FileSystem filter language in about_Wildcards. Filters are more efficient than other parameters, because the provider applies them when the cmdlet gets the objects rather than having PowerShell filter the objects after they are retrieved.

参数属性

类型:String
默认值:None
支持通配符:True
不显示:False

参数集

(All)
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

-Force

Forces the cmdlet to set items that cannot otherwise be changed, such as read-only alias or variables. The cmdlet cannot change constant aliases or variables. Implementation varies from provider to provider. For more information, see about_Providers. Even using the Force parameter, the cmdlet cannot override security restrictions.

参数属性

类型:SwitchParameter
默认值:False
支持通配符:False
不显示:False

参数集

(All)
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

-Include

Specifies, as a string array, an item or items that this cmdlet includes in the operation. The value of this parameter qualifies the Path parameter. Enter a path element or pattern, such as "*.txt". Wildcard characters are permitted. The Include parameter is effective only when the command includes the contents of an item, such as C:\Windows\*, where the wildcard character specifies the contents of the C:\Windows directory.

参数属性

类型:

String[]

默认值:None
支持通配符:True
不显示:False

参数集

(All)
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

-LiteralPath

Specifies a path to one or more locations. The value of LiteralPath is used exactly as it is typed. No characters are interpreted as wildcards. If the path includes escape characters, enclose it in single quotation marks. Single quotation marks tell PowerShell not to interpret any characters as escape sequences.

For more information, see about_Quoting_Rules.

参数属性

类型:

String[]

默认值:None
支持通配符:False
不显示:False
别名:PSPath

参数集

LiteralPath
Position:Named
必需:True
来自管道的值:False
来自管道的值(按属性名称):True
来自剩余参数的值:False

-PassThru

Passes an object that represents the item to the pipeline. By default, this cmdlet does not generate any output.

参数属性

类型:SwitchParameter
默认值:False
支持通配符:False
不显示:False

参数集

(All)
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

-Path

Specifies a path of the location of the items. Wildcard characters are permitted.

参数属性

类型:

String[]

默认值:None
支持通配符:True
不显示:False

参数集

Path
Position:0
必需:True
来自管道的值:False
来自管道的值(按属性名称):True
来自剩余参数的值:False

-UseTransaction

Includes the command in the active transaction. This parameter is valid only when a transaction is in progress. For more information, see about_Transactions.

参数属性

类型:SwitchParameter
默认值:False
支持通配符:False
不显示:False
别名:usetx

参数集

(All)
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

-Value

Specifies a new value for the item.

参数属性

类型:Object
默认值:None
支持通配符:False
不显示:False

参数集

(All)
Position:1
必需:False
来自管道的值:True
来自管道的值(按属性名称):True
来自剩余参数的值:False

-WhatIf

Shows what would happen if the cmdlet runs. The cmdlet is not run.

参数属性

类型:SwitchParameter
默认值:False
支持通配符:False
不显示:False
别名:wi

参数集

(All)
Position:Named
必需:False
来自管道的值:False
来自管道的值(按属性名称):False
来自剩余参数的值:False

CommonParameters

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutBuffer, -OutVariable, -PipelineVariable, -ProgressAction, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.

输入

Object

You can pipe an object that represents the new value of the item to this cmdlet.

输出

None or an object representing the new or changed item.

This cmdlet generates an object that represent the item, if you specify the PassThru parameter. Otherwise, this cmdlet does not generate any output.

备注

  • Set-Item is not supported by the PowerShell FileSystem provider. To change the values of items in the file system, use the Set-Content cmdlet.
  • In the Registry drives, HKLM: and HKCU:, Set-Item changes the data in the (Default) value of a registry key.
    • To create and change the names of registry keys, use the New-Item and Rename-Item cmdlet.
    • To change the names and data in registry values, use the New-ItemProperty, Set-ItemProperty, and Rename-ItemProperty cmdlets.
  • Set-Item is designed to work with the data exposed by any provider. To list the providers available in your session, type Get-PsProvider. For more information, see about_Providers.