Set-ItemProperty
創造或改變物品屬性的價值。
語法
propertyValuePathSet (Default) - All providers
Set-ItemProperty
[-Path] <string[]>
[-Name] <string>
[-Value] <Object>
[-PassThru]
[-Force]
[-Filter <string>]
[-Include <string[]>]
[-Exclude <string[]>]
[-Credential <pscredential>]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
propertyPSObjectPathSet - All providers
Set-ItemProperty
[-Path] <string[]>
-InputObject <psobject>
[-PassThru]
[-Force]
[-Filter <string>]
[-Include <string[]>]
[-Exclude <string[]>]
[-Credential <pscredential>]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
propertyValueLiteralPathSet - All providers
Set-ItemProperty
[-Name] <string>
[-Value] <Object>
-LiteralPath <string[]>
[-PassThru]
[-Force]
[-Filter <string>]
[-Include <string[]>]
[-Exclude <string[]>]
[-Credential <pscredential>]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
propertyPSObjectLiteralPathSet - All providers
Set-ItemProperty
-LiteralPath <string[]>
-InputObject <psobject>
[-PassThru]
[-Force]
[-Filter <string>]
[-Include <string[]>]
[-Exclude <string[]>]
[-Credential <pscredential>]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
propertyValuePathSet (Default) - Registry provider
Set-ItemProperty
[-Path] <string[]>
[-Name] <string>
[-Value] <Object>
[-PassThru]
[-Force]
[-Filter <string>]
[-Include <string[]>]
[-Exclude <string[]>]
[-Credential <pscredential>]
[-WhatIf]
[-Confirm]
[-Type <RegistryValueKind>]
[<CommonParameters>]
propertyPSObjectPathSet - Registry provider
Set-ItemProperty
[-Path] <string[]>
-InputObject <psobject>
[-PassThru]
[-Force]
[-Filter <string>]
[-Include <string[]>]
[-Exclude <string[]>]
[-Credential <pscredential>]
[-WhatIf]
[-Confirm]
[-Type <RegistryValueKind>]
[<CommonParameters>]
propertyValueLiteralPathSet - Registry provider
Set-ItemProperty
[-Name] <string>
[-Value] <Object>
-LiteralPath <string[]>
[-PassThru]
[-Force]
[-Filter <string>]
[-Include <string[]>]
[-Exclude <string[]>]
[-Credential <pscredential>]
[-WhatIf]
[-Confirm]
[-Type <RegistryValueKind>]
[<CommonParameters>]
propertyPSObjectLiteralPathSet - Registry provider
Set-ItemProperty
-LiteralPath <string[]>
-InputObject <psobject>
[-PassThru]
[-Force]
[-Filter <string>]
[-Include <string[]>]
[-Exclude <string[]>]
[-Credential <pscredential>]
[-WhatIf]
[-Confirm]
[-Type <RegistryValueKind>]
[<CommonParameters>]
Description
Set-ItemProperty指令檔會改變指定項目屬性的值。
你可以用 cmdlet 來建立或更改項目的屬性。
例如,你可以用 Set-ItemProperty 來將檔案物件的 IsReadOnly 屬性設定為 $true。
你也可以用來 Set-ItemProperty 建立和更改登錄檔值和資料。
例如,你可以為某個鍵新增登錄檔條目,並建立或更改其值。
範例
範例 1:設定檔案的屬性
此指令將「final.doc」檔案的 IsReadOnly 屬性值設為「true」。 它使用 Path 來指定檔案,使用 Name 指定屬性名稱,以及 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-Item or Get-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-ItemPropertyName 和 Value 參數來指定屬性及其新值。
Get-ChildItem weekly.txt | Set-ItemProperty -Name IsReadOnly -Value $true
參數
-Confirm
在執行 Cmdlet 之前,提示您進行確認。
參數屬性
| 類型: | SwitchParameter |
| 預設值: | False |
| 支援萬用字元: | False |
| 不要顯示: | False |
| 別名: | 參見 |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-Credential
Note
任何與 PowerShell 一起安裝的提供者都不支援此參數。 若要模擬其他使用者,或在執行此 Cmdlet 時提升您的認證,請使用 Invoke-Command。
參數屬性
| 類型: | PSCredential |
| 預設值: | Current user |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
-Exclude
指定要在此 Cmdlet 作業中排除的項目或多項,並以字串陣列形式表示。 此參數的值對 路徑 參數進行限定。 輸入路徑元素或模式,例如 *.txt。 允許使用通配符字元。 只有在命令包含項目的內容,例如 時,C:\Windows\* 參數才有效,其中通配符會指定 C:\Windows 目錄的內容。
參數屬性
| 類型: | String[] |
| 預設值: | None |
| 支援萬用字元: | True |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-Filter
指定篩選條件,以限定 Path 參數。 FileSystem 提供者是唯一已安裝且支援使用篩選的 PowerShell 提供者。 您可以在 about_Wildcards中找到 FileSystem 篩選語言的語法。 篩選比其他參數更有效率,因為提供者會在 Cmdlet 取得物件時套用它們,而不是在擷取對象之後讓 PowerShell 篩選物件。
參數屬性
| 類型: | String |
| 預設值: | None |
| 支援萬用字元: | True |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-Force
強制指令長對使用者無法存取的項目設定屬性。 實作會因提供者而異。 如需詳細資訊,請參閱 about_Providers。
參數屬性
| 類型: | SwitchParameter |
| 預設值: | False |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-Include
指定此 Cmdlet 在操作中包含的一個或多個專案,這些專案是以字串陣列的形式表示。 此參數的值對 路徑 參數進行限定。 輸入路徑元素或模式,例如 "*.txt"。 允許使用通配符字元。 只有當命令包含項目的內容,例如 時,C:\Windows\* 參數才有效,其中通配符會指定 C:\Windows 目錄的內容。
參數屬性
| 類型: | String[] |
| 預設值: | None |
| 支援萬用字元: | True |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-InputObject
指定該 cmdlet 所改變屬性的物件。 輸入包含該物件的變數,或輸入取得該物件的指令。
參數屬性
| 類型: | PSObject |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
propertyPSObjectPathSet
| Position: | Named |
| 必要: | True |
| 來自管線的值: | True |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
propertyPSObjectLiteralPathSet
| Position: | Named |
| 必要: | True |
| 來自管線的值: | True |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
-LiteralPath
指定通往一個或多個位置的路徑。 LiteralPath 的值會按原本輸入的方式使用。 不會將任何字元解譯為通配符。 如果路徑包含逸出字元,請以單引弧括住它。 單引號會告知PowerShell不要將任何字元解譯為逸出序列。
如需詳細資訊,請參閱 about_Quoting_Rules。
參數屬性
| 類型: | String[] |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
| 別名: | PSPath, LP |
參數集
propertyValueLiteralPathSet
| Position: | Named |
| 必要: | True |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
propertyPSObjectLiteralPathSet
| Position: | Named |
| 必要: | True |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
-Name
指定物業名稱。
參數屬性
| 類型: | String |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
| 別名: | PSProperty |
參數集
propertyValuePathSet
| Position: | 1 |
| 必要: | True |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
propertyValueLiteralPathSet
| Position: | 1 |
| 必要: | True |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
-PassThru
回傳一個代表該項目屬性的物件。 根據預設,此 Cmdlet 不會產生任何輸出。
參數屬性
| 類型: | SwitchParameter |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-Path
指定項目的路徑,並指定可修改的屬性。 允許使用通配符字元。
參數屬性
| 類型: | String[] |
| 預設值: | None |
| 支援萬用字元: | True |
| 不要顯示: | False |
參數集
propertyValuePathSet
| Position: | 0 |
| 必要: | True |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
propertyPSObjectPathSet
| Position: | 0 |
| 必要: | True |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
-Type
這是由 登錄檔 提供者提供的動態參數。 Registry提供者及此參數僅在 Windows 上提供。
指定此 cmdlet 所新增的屬性類型。 此參數可接受的值為:
-
String: 指定一個空終端字串。 用於 REG_SZ 數值。 -
ExpandString: 指定一個空終止字串,包含未展開的環境變數參考,當取回該值時會展開。 用於 REG_EXPAND_SZ 數值。 -
Binary: 指定任何形式的二進位資料。 用於 REG_BINARY 數值。 -
DWord: 指定一個 32 位元的二進位數。 用於 REG_DWORD 數值。 -
MultiString: 指定一個由兩個空字元結尾的空終止字串陣列。 用於 REG_MULTI_SZ 數值。 -
Qword: 指定一個 64 位元的二進位數。 用於 REG_QWORD 數值。 -
Unknown表示不支援的登錄資料型態,例如 REG_RESOURCE_LIST 值。
參數屬性
| 類型: | RegistryValueKind |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
-Value
指定該房產的價值。
參數屬性
| 類型: | Object |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
propertyValuePathSet
| Position: | 2 |
| 必要: | True |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
propertyValueLiteralPathSet
| Position: | 2 |
| 必要: | True |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | True |
| 來自剩餘引數的值: | False |
-WhatIf
顯示 Cmdlet 執行時會發生什麼事。 指令未執行。
參數屬性
| 類型: | SwitchParameter |
| 預設值: | False |
| 支援萬用字元: | False |
| 不要顯示: | False |
| 別名: | 威斯康辛 |
參數集
(All)
| Position: | Named |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
CommonParameters
此 cmdlet 支援常見參數:-Debug、-ErrorAction、-ErrorVariable、-InformationAction、-InformationVariable、-OutBuffer、-OutVariable、-PipelineVariable、-ProgressAction、-Verbose、-WarningAction 和 -WarningVariable。 如需詳細資訊,請參閱 about_CommonParameters。
輸入
PSObject
您可以將物件通過管線傳送至此 Cmdlet。
輸出
None
根據預設,此 Cmdlet 不會傳回任何輸出。
PSCustomObject
當你使用 PassThru 參數時,這個 cmdlet 會回傳一個 PSCustomObject 物件,代表被更改的項目及其新的屬性值。
備註
PowerShell 包含下列 Set-ItemProperty的別名:
- 所有平臺:
sp
Set-ItemProperty 的設計目的是要處理任何提供者所公開的數據。 若要列出工作階段中可用的提供者,請輸入 Get-PSProvider。 如需詳細資訊,請參閱 about_Providers。