Set-ItemProperty

建立或變更專案屬性的值。

Syntax

Set-ItemProperty
   [-Path] <string[]>
   [-Name] <string>
   [-Value] <Object>
   [-PassThru]
   [-Force]
   [-Filter <string>]
   [-Include <string[]>]
   [-Exclude <string[]>]
   [-Credential <pscredential>]
   [-WhatIf]
   [-Confirm]
   [-UseTransaction]
   [<CommonParameters>]
Set-ItemProperty
   [-Path] <string[]>
   -InputObject <psobject>
   [-PassThru]
   [-Force]
   [-Filter <string>]
   [-Include <string[]>]
   [-Exclude <string[]>]
   [-Credential <pscredential>]
   [-WhatIf]
   [-Confirm]
   [-UseTransaction]
   [<CommonParameters>]
Set-ItemProperty
   [-Name] <string>
   [-Value] <Object>
   -LiteralPath <string[]>
   [-PassThru]
   [-Force]
   [-Filter <string>]
   [-Include <string[]>]
   [-Exclude <string[]>]
   [-Credential <pscredential>]
   [-WhatIf]
   [-Confirm]
   [-UseTransaction]
   [<CommonParameters>]
Set-ItemProperty
   -LiteralPath <string[]>
   -InputObject <psobject>
   [-PassThru]
   [-Force]
   [-Filter <string>]
   [-Include <string[]>]
   [-Exclude <string[]>]
   [-Credential <pscredential>]
   [-WhatIf]
   [-Confirm]
   [-UseTransaction]
   [<CommonParameters>]
Set-ItemProperty
   [-Path] <string[]>
   [-Name] <string>
   [-Value] <Object>
   [-PassThru]
   [-Force]
   [-Filter <string>]
   [-Include <string[]>]
   [-Exclude <string[]>]
   [-Credential <pscredential>]
   [-WhatIf]
   [-Confirm]
   [-UseTransaction]
   [-Type <RegistryValueKind>]
   [<CommonParameters>]
Set-ItemProperty
   [-Path] <string[]>
   -InputObject <psobject>
   [-PassThru]
   [-Force]
   [-Filter <string>]
   [-Include <string[]>]
   [-Exclude <string[]>]
   [-Credential <pscredential>]
   [-WhatIf]
   [-Confirm]
   [-UseTransaction]
   [-Type <RegistryValueKind>]
   [<CommonParameters>]
Set-ItemProperty
   [-Name] <string>
   [-Value] <Object>
   -LiteralPath <string[]>
   [-PassThru]
   [-Force]
   [-Filter <string>]
   [-Include <string[]>]
   [-Exclude <string[]>]
   [-Credential <pscredential>]
   [-WhatIf]
   [-Confirm]
   [-UseTransaction]
   [-Type <RegistryValueKind>]
   [<CommonParameters>]
Set-ItemProperty
   -LiteralPath <string[]>
   -InputObject <psobject>
   [-PassThru]
   [-Force]
   [-Filter <string>]
   [-Include <string[]>]
   [-Exclude <string[]>]
   [-Credential <pscredential>]
   [-WhatIf]
   [-Confirm]
   [-UseTransaction]
   [-Type <RegistryValueKind>]
   [<CommonParameters>]

Description

Cmdlet 會 Set-ItemProperty 變更指定項目的 屬性值。 您可以使用 Cmdlet 來建立或變更項目的屬性。 例如,您可以使用 Set-ItemProperty 將檔案物件的 IsReadOnly 屬性值設定$True

您也可以使用 Set-ItemProperty 來建立和變更登錄值和數據。 例如,您可以將新的登錄專案新增至機碼,並建立或變更其值。

範例

範例 1:設定檔案的屬性

此命令會將 「final.doc」 檔案的 IsReadOnly 屬性值設定為 「true」。 它會使用 Path 來指定檔案、 指定 屬性的名稱,以及 指定新值的 Value 參數。

檔案是 System.IO.FileInfo 物件, 而 IsReadOnly 只是它的其中一個屬性。 若要檢視所有屬性,請輸入 Get-Item C:\GroupFiles\final.doc | Get-Member -MemberType Property

自動 $true 變數代表 「TRUE」 的值。 如需詳細資訊,請參閱 about_Automatic_Variables

Set-ItemProperty -Path C:\GroupFiles\final.doc -Name IsReadOnly -Value $true

範例 2:建立登錄專案和值

此範例示範如何使用 Set-ItemProperty 來建立新的登錄專案,並將值指派給專案。 它會在機碼的 「ContosoCompany」 機碼 HKLM\Software 中建立 「NoOfEmployees」 專案,並將其值設定為 823。

因為登錄專案會被視為登錄機碼的屬性,也就是專案,因此您可以使用 Set-ItemProperty 來建立登錄專案,以及建立和變更其值。

Set-ItemProperty -Path "HKLM:\Software\ContosoCompany" -Name "NoOfEmployees" -Value 823
Get-ItemProperty -Path "HKLM:\Software\ContosoCompany"

PSPath        : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\software\contosocompany
PSParentPath  : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\software
PSChildName   : contosocompany
PSDrive       : HKLM
PSProvider    : Microsoft.PowerShell.Core\Registry
NoOfLocations : 2
NoOfEmployees : 823

Set-ItemProperty -Path "HKLM:\Software\ContosoCompany" -Name "NoOfEmployees" -Value 824
Get-ItemProperty -Path "HKLM:\Software\ContosoCompany"

PSPath        : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\software\contosocompany
PSParentPath  : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\software
PSChildName   : contosocompany
PSDrive       : HKLM
PSProvider    : Microsoft.PowerShell.Core\Registry
NoOfLocations : 2
NoOfEmployees : 824

第一個命令會建立登錄專案。 它會使用Path來指定磁碟驅動器和Software\MyCompany金鑰的路徑HKLM:。 命令會使用 Name 來指定項目名稱和 Value 來指定值。

第二個命令會 Get-ItemProperty 使用 Cmdlet 來查看新的登錄專案。 如果您使用 Get-ItemGet-ChildItem Cmdlet,則專案不會顯示,因為它們是索引鍵的屬性,而不是專案或子專案。

第三個命令會將 NoOfEmployees 專案的值變更為 824。

您也可以使用 New-ItemProperty Cmdlet 來建立登錄專案及其值,然後使用 Set-ItemProperty 來變更值。

如需磁碟驅動器的詳細資訊 HKLM: ,請輸入 Get-Help Get-PSDrive。 如需如何使用 PowerShell 管理登錄的詳細資訊,請輸入 Get-Help Registry

範例 3:使用管線修改專案

此範例會使用 Get-ChildItem 來取得 weekly.txt 檔案。 檔案物件會管道傳送至 Set-ItemProperty。 此命令 Set-ItemProperty使用 NameValue 參數來指定 屬性及其新值。

Get-ChildItem weekly.txt | Set-ItemProperty -Name IsReadOnly -Value $True

參數

-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 在用戶無法存取的項目上設定屬性。 實作會因提供者而異。 如需詳細資訊,請參閱 about_Providers

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

-InputObject

指定具有這個 Cmdlet 變更之屬性的物件。 輸入包含 物件的變數,或取得物件的命令。

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

-LiteralPath

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

如需詳細資訊,請參閱 about_Quoting_Rules

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

-Name

指定屬性的名稱。

Type:String
Aliases:PSProperty
Position:1
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:False

-PassThru

傳回物件,表示項目屬性。 根據預設,此 Cmdlet 不會產生任何輸出。

Type:SwitchParameter
Position:Named
Default value:None
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

-UseTransaction

在作用中交易中包含 命令。 只有在交易進行中時,此參數才有效。 如需詳細資訊,請參閱 about_Transactions

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

-Value

指定屬性的值。

Type:Object
Position:2
Default value:None
Required:True
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

輸入

PSObject

您可以使用管線將物件傳送至此 Cmdlet。

輸出

None

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

PSCustomObject

當您使用 PassThru 參數時,這個 Cmdlet 會 傳回 PSCustomObject 物件,代表已變更的專案及其新屬性值。

備註

Windows PowerShell 包含下列的 Set-ItemProperty別名:

  • sp

Set-ItemProperty 是設計來使用任何提供者所公開的數據。 若要列出工作階段中可用的提供者,請輸入 Get-PSProvider。 如需詳細資訊,請參閱 about_Providers