Set-ItemProperty
建立或變更專案屬性的值。
語法
Set-ItemProperty
[-Path] <string[]>
[-Name] <string>
[-Value] <Object>
[-PassThru]
[-Force]
[-Filter <string>]
[-Include <string[]>]
[-Exclude <string[]>]
[-Credential <pscredential>]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
Set-ItemProperty
[-Path] <string[]>
-InputObject <psobject>
[-PassThru]
[-Force]
[-Filter <string>]
[-Include <string[]>]
[-Exclude <string[]>]
[-Credential <pscredential>]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
Set-ItemProperty
[-Name] <string>
[-Value] <Object>
-LiteralPath <string[]>
[-PassThru]
[-Force]
[-Filter <string>]
[-Include <string[]>]
[-Exclude <string[]>]
[-Credential <pscredential>]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
Set-ItemProperty
-LiteralPath <string[]>
-InputObject <psobject>
[-PassThru]
[-Force]
[-Filter <string>]
[-Include <string[]>]
[-Exclude <string[]>]
[-Credential <pscredential>]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
Set-ItemProperty
[-Path] <string[]>
[-Name] <string>
[-Value] <Object>
[-PassThru]
[-Force]
[-Filter <string>]
[-Include <string[]>]
[-Exclude <string[]>]
[-Credential <pscredential>]
[-WhatIf]
[-Confirm]
[-Type <RegistryValueKind>]
[<CommonParameters>]
Set-ItemProperty
[-Path] <string[]>
-InputObject <psobject>
[-PassThru]
[-Force]
[-Filter <string>]
[-Include <string[]>]
[-Exclude <string[]>]
[-Credential <pscredential>]
[-WhatIf]
[-Confirm]
[-Type <RegistryValueKind>]
[<CommonParameters>]
Set-ItemProperty
[-Name] <string>
[-Value] <Object>
-LiteralPath <string[]>
[-PassThru]
[-Force]
[-Filter <string>]
[-Include <string[]>]
[-Exclude <string[]>]
[-Credential <pscredential>]
[-WhatIf]
[-Confirm]
[-Type <RegistryValueKind>]
[<CommonParameters>]
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 會變更指定項目的 屬性值。
您可以使用 Cmdlet 來建立或變更項目的屬性。
例如,您可以使用 Set-ItemProperty
,將檔案物件的 IsReadOn ly 屬性的值設定為 $True
。
您也可以使用 Set-ItemProperty
來建立和變更登錄值和數據。
例如,您可以將新的登錄專案新增至機碼,並建立或變更其值。
範例
範例 1:設定檔案的屬性
此命令會將 「final.doc」 檔案的 IsReadOn ly 屬性的值設定為 “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
建立新的登錄專案,並將值指派給專案。 它會在 HKLM\Software
索引鍵的 「ContosoCompany」 索引鍵中建立 「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 來指定 HKLM:
磁碟驅動器的路徑和 Software\MyCompany
金鑰。
此命令會使用 Name 來指定專案名稱和 Value 來指定值。
第二個命令會使用 Get-ItemProperty
Cmdlet 來查看新的登錄專案。
如果您使用 Get-Item
或 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-ItemProperty
命令會使用 Name 和 Value 參數來指定 屬性及其新值。
Get-ChildItem weekly.txt | Set-ItemProperty -Name IsReadOnly -Value $True
參數
-Confirm
在執行 Cmdlet 之前,提示您進行確認。
類型: | SwitchParameter |
別名: | cf |
Position: | Named |
預設值: | False |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | False |
-Credential
注意
任何與 PowerShell 一起安裝的提供者都不支援此參數。 若要模擬其他使用者,或在執行此 Cmdlet 時提升您的認證,請使用 Invoke-Command。
類型: | PSCredential |
Position: | Named |
預設值: | Current user |
必要: | False |
接受管線輸入: | True |
接受萬用字元: | False |
-Exclude
指定此 Cmdlet 在作業中排除的專案或專案,做為字串陣列。 此參數的值會限定 path 參數。 輸入路徑專案或模式,例如 *.txt
。 允許通配符。 只有在命令包含項目的內容,例如 C:\Windows\*
時,Exclude 參數才有效,其中通配符會指定 C:\Windows
目錄的內容。
類型: | String[] |
Position: | Named |
預設值: | None |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | True |
-Filter
指定篩選條件,以限定 Path 參數。 FileSystem 提供者是唯一支援使用篩選的 PowerShell 提供者。 您可以在 about_Wildcards中找到 FileSystem 篩選語言的語法。 篩選比其他參數更有效率,因為提供者會在 Cmdlet 取得物件時套用它們,而不是在擷取對象之後讓 PowerShell 篩選物件。
類型: | String |
Position: | Named |
預設值: | None |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | True |
-Force
強制 Cmdlet 在用戶無法存取的項目上設定屬性。 實作會因提供者而異。 如需詳細資訊,請參閱 about_Providers。
類型: | SwitchParameter |
Position: | Named |
預設值: | False |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | False |
-Include
指定此 Cmdlet 包含在作業中的專案,做為字串陣列。 此參數的值會限定 path 參數。 輸入路徑專案或模式,例如 "*.txt"
。 允許通配符。 只有當命令包含項目的內容,例如 C:\Windows\*
時,Include 參數才有效,其中通配符會指定 C:\Windows
目錄的內容。
類型: | String[] |
Position: | Named |
預設值: | None |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | True |
-InputObject
指定具有這個 Cmdlet 變更之屬性的物件。 輸入包含 物件的變數,或取得物件的命令。
類型: | PSObject |
Position: | Named |
預設值: | None |
必要: | True |
接受管線輸入: | True |
接受萬用字元: | False |
-LiteralPath
指定一或多個位置的路徑。 LiteralPath 的值會與類型完全相同使用。 不會將任何字元解譯為通配符。 如果路徑包含逸出字元,請以單引弧括住它。 單引號會告知PowerShell不要將任何字元解譯為逸出序列。
如需詳細資訊,請參閱 about_Quoting_Rules。
類型: | String[] |
別名: | PSPath, LP |
Position: | Named |
預設值: | None |
必要: | True |
接受管線輸入: | True |
接受萬用字元: | False |
-Name
指定屬性的名稱。
類型: | String |
別名: | PSProperty |
Position: | 1 |
預設值: | None |
必要: | True |
接受管線輸入: | True |
接受萬用字元: | False |
-PassThru
傳回物件,表示項目屬性。 根據預設,此 Cmdlet 不會產生任何輸出。
類型: | SwitchParameter |
Position: | Named |
預設值: | None |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | False |
-Path
指定要修改之屬性的項目路徑。 允許通配符。
類型: | String[] |
Position: | 0 |
預設值: | None |
必要: | True |
接受管線輸入: | True |
接受萬用字元: | 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 值。
類型: | RegistryValueKind |
Position: | Named |
預設值: | None |
必要: | False |
接受管線輸入: | True |
接受萬用字元: | False |
-Value
指定屬性的值。
類型: | Object |
Position: | 2 |
預設值: | None |
必要: | True |
接受管線輸入: | True |
接受萬用字元: | False |
-WhatIf
顯示 Cmdlet 執行時會發生什麼事。 Cmdlet 未執行。
類型: | SwitchParameter |
別名: | wi |
Position: | Named |
預設值: | False |
必要: | False |
接受管線輸入: | False |
接受萬用字元: | False |
輸入
您可以使用管線將物件傳送至此 Cmdlet。
輸出
None
根據預設,此 Cmdlet 不會傳回任何輸出。
當您使用 PassThru 參數時,這個 Cmdlet 會傳回 PSCustomObject 物件,代表已變更的專案及其新的屬性值。
備註
PowerShell 包含下列 Set-ItemProperty
別名:
- 所有平臺:
sp
Set-ItemProperty
的設計目的是要處理任何提供者所公開的數據。 若要列出工作階段中可用的提供者,請輸入 Get-PSProvider
。 如需詳細資訊,請參閱 about_Providers。