Set-Item

將專案的值變更為命令中指定的值。

Syntax

Set-Item
   [-Path] <String[]>
   [[-Value] <Object>]
   [-Force]
   [-PassThru]
   [-Filter <String>]
   [-Include <String[]>]
   [-Exclude <String[]>]
   [-Credential <PSCredential>]
   [-WhatIf]
   [-Confirm]
   [<CommonParameters>]
Set-Item
   -LiteralPath <String[]>
   [[-Value] <Object>]
   [-Force]
   [-PassThru]
   [-Filter <String>]
   [-Include <String[]>]
   [-Exclude <String[]>]
   [-Credential <PSCredential>]
   [-WhatIf]
   [-Confirm]
   [<CommonParameters>]
Set-Item
   [-Path] <string[]>
   [[-Value] <Object>]
   [-Force]
   [-PassThru]
   [-Filter <string>]
   [-Include <string[]>]
   [-Exclude <string[]>]
   [-Credential <pscredential>]
   [-WhatIf]
   [-Confirm]
   [-Options <ScopedItemOptions>]
   [<CommonParameters>]
Set-Item
   [[-Value] <Object>]
   -LiteralPath <string[]>
   [-Force]
   [-PassThru]
   [-Filter <string>]
   [-Include <string[]>]
   [-Exclude <string[]>]
   [-Credential <pscredential>]
   [-WhatIf]
   [-Confirm]
   [-Options <ScopedItemOptions>]
   [<CommonParameters>]
Set-Item
   [-Path] <string[]>
   [[-Value] <Object>]
   [-Force]
   [-PassThru]
   [-Filter <string>]
   [-Include <string[]>]
   [-Exclude <string[]>]
   [-Credential <pscredential>]
   [-WhatIf]
   [-Confirm]
   [-Type <RegistryValueKind>]
   [<CommonParameters>]
Set-Item
   [[-Value] <Object>]
   -LiteralPath <string[]>
   [-Force]
   [-PassThru]
   [-Filter <string>]
   [-Include <string[]>]
   [-Exclude <string[]>]
   [-Credential <pscredential>]
   [-WhatIf]
   [-Confirm]
   [-Type <RegistryValueKind>]
   [<CommonParameters>]

Description

Cmdlet 會將 Set-Item 專案的值,例如變數或登錄機碼,變更為命令中指定的值。

範例

範例 1:建立別名

此命令會為 記事本 建立 np 的別名。

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

範例 2:變更環境變數的值

此命令會將 UserRole 環境變數的值變更為 管理員 istrator。

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

範例 3:修改您的提示函式

此命令會變更提示函式,使其顯示路徑之前的時間。

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

範例 4:設定提示函式的選項

此命令會設定提示函式的 AllScopeReadOnly 選項。 此命令使用Set-ItemOptions 動態參數。 只有在您將Options參數與 AliasFunction 提供者搭配使用時,Set-Item才能使用。

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

參數

-Confirm

執行 Cmdlet 之前先提示您確認。

Type:SwitchParameter
Aliases:cf
Position:Named
Default value:False
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-Credential

注意

任何與 PowerShell 一起安裝的提供者都不支援此參數。 若要模擬其他使用者,或在執行此 Cmdlet 時提升您的認證,請使用 Invoke-Command

Type:PSCredential
Position:Named
Default value:Current user
Required:False
Accept pipeline input:True
Accept wildcard characters:False

-Exclude

指定此 Cmdlet 在作業中排除的專案或專案,做為字串陣列。 此參數的值會 限定Path 參數。 輸入路徑專案或模式,例如 *.txt。 允許通配符。 Exclude 參數只有在命令包含項目的內容時有效,例如 C:\Windows\*,通配符會指定目錄的內容C:\Windows

Type:String[]
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:True

-Filter

指定篩選條件以限定 Path 參數。 FileSystem 提供者是唯一支援使用篩選器的已安裝 PowerShell 提供者。 您可以在 about_Wildcards 中找到 FileSystem 篩選語言的語法。 篩選比其他參數更有效率,因為提供者會在 Cmdlet 取得物件時套用它們,而不是在擷取對象之後讓 PowerShell 篩選物件。

Type:String
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:True

-Force

強制 Cmdlet 設定無法變更的專案,例如唯讀別名或變數。 Cmdlet 無法變更常數別名或變數。 實作會因提供者而異。 如需詳細資訊,請參閱 about_Providers。 即使使用 Force 參數,Cmdlet 也無法覆寫安全性限制。

Type:SwitchParameter
Position:Named
Default value:False
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-Include

指定此 Cmdlet 包含在作業中的專案,做為字串陣列。 此參數的值會 限定Path 參數。 輸入路徑專案或模式,例如 "*.txt"。 允許通配符。 Include 參數只有在命令包含項目的內容時有效,例如 C:\Windows\*,其中通配符會指定目錄的內容C:\Windows

Type:String[]
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:True

-LiteralPath

指定一或多個位置的路徑。 LiteralPath 的值會與類型完全相同使用。 不會將任何字元解譯為通配符。 如果路徑包含逸出字元,請以單引弧括住它。 單引號會告知PowerShell不要將任何字元解譯為逸出序列。

如需詳細資訊,請參閱 about_Quoting_Rules

Type:String[]
Aliases:PSPath, LP
Position:Named
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:False

-Options

這是別名式提供者所提供的動態參數。 如需詳細資訊,請參閱 about_Alias_Providerabout_Function_Provider

指定別名的 Options 屬性值。

有效值為:

  • None:別名沒有條件約束(預設值)
  • ReadOnly:可以使用 Force 參數刪除別名,但無法變更
  • Constant:無法刪除或變更別名
  • Private:別名只能在目前的範圍內使用
  • AllScope:別名會複製到任何已建立的新範圍
  • Unspecified:未指定選項
Type:ScopedItemOptions
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-PassThru

將代表專案的對象傳遞至管線。 根據預設,此 Cmdlet 不會產生任何輸出。

Type:SwitchParameter
Position:Named
Default value:False
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-Path

指定專案位置的路徑。 允許通配符。

Type:String[]
Position:0
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:True

-Type

這是登錄提供者提供的動態參數。 登錄提供者和此參數僅適用於 Windows。

指定這個 Cmdlet 新增的屬性類型。 此參數可接受的值為:

  • String:指定以 Null 結尾的字串。 用於 REG_SZ 值。
  • ExpandString:指定以 Null 結束的字串,其中包含擷取值時展開之環境變數的未展開參考。 用於 REG_EXPAND_SZ 值。
  • Binary:以任何形式指定二進位數據。 用於 REG_BINARY 值。
  • DWord:指定32位二進位數。 用於 REG_DWORD 值。
  • MultiString:指定以兩個 Null 字元結尾的 Null 終止字串陣列。 用於 REG_MULTI_SZ 值。
  • Qword:指定 64 位二進位數。 用於 REG_QWORD 值。
  • Unknown:表示不支援的登錄數據類型,例如 REG_RESOURCE_LIST 值。
Type:RegistryValueKind
Position:Named
Default value:None
Required:False
Accept pipeline input:True
Accept wildcard characters:False

-Value

指定專案的新值。

Type:Object
Position:1
Default value:None
Required:False
Accept pipeline input:True
Accept wildcard characters:False

-WhatIf

顯示執行 Cmdlet 後會發生的情況。 未執行 Cmdlet。

Type:SwitchParameter
Aliases:wi
Position:Named
Default value:False
Required:False
Accept pipeline input:False
Accept wildcard characters:False

輸入

Object

您可以使用管線將代表專案新值的物件傳送至這個 Cmdlet。

輸出

None

根據預設,此 Cmdlet 不會傳回任何輸出。

Object

當您使用 PassThru 參數時,這個 Cmdlet 會傳回代表項目的物件。

備註

PowerShell 包含下列的 Set-Item別名:

  • 所有平臺:

    • si
  • Set-Item PowerShell FileSystem 提供者不支援。 若要變更文件系統中的專案值,請使用 Set-Content Cmdlet。

  • 在 [登錄磁碟驅動器] 和 Set-ItemHKCU:中,HKLM:變更登錄機碼之 [預設值] 值中的數據。

    • 若要建立和變更登錄機碼的名稱,請使用 New-ItemRename-Item Cmdlet。
    • 若要變更登錄值中的名稱和數據,請使用 New-ItemPropertySet-ItemPropertyRename-ItemProperty Cmdlet。
  • Set-Item 是設計來使用任何提供者所公開的數據。 若要列出工作階段中可用的提供者,請輸入 Get-PsProvider。 如需詳細資訊,請參閱 about_Providers