共用方式為


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:Create 別名

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

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

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

此命令會將 UserRole 環境變數的值變更為 Administrator。

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參數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。

  • 在登錄磁碟驅動器和 HKCU:中, Set-ItemHKLM:變更登錄機碼 (預設) 值中的數據。

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