共用方式為


Set-LocalUser

修改本機用戶帳戶。

語法

Set-LocalUser
   [-AccountExpires <DateTime>]
   [-AccountNeverExpires]
   [-Description <String>]
   [-FullName <String>]
   [-Name] <String>
   [-Password <SecureString>]
   [-PasswordNeverExpires <Boolean>]
   [-UserMayChangePassword <Boolean>]
   [-WhatIf]
   [-Confirm]
   [<CommonParameters>]
Set-LocalUser
   [-AccountExpires <DateTime>]
   [-AccountNeverExpires]
   [-Description <String>]
   [-FullName <String>]
   [-InputObject] <LocalUser>
   [-Password <SecureString>]
   [-PasswordNeverExpires <Boolean>]
   [-UserMayChangePassword <Boolean>]
   [-WhatIf]
   [-Confirm]
   [<CommonParameters>]
Set-LocalUser
   [-AccountExpires <DateTime>]
   [-AccountNeverExpires]
   [-Description <String>]
   [-FullName <String>]
   [-Password <SecureString>]
   [-PasswordNeverExpires <Boolean>]
   [-SID] <SecurityIdentifier>
   [-UserMayChangePassword <Boolean>]
   [-WhatIf]
   [-Confirm]
   [<CommonParameters>]

Description

Set-LocalUser Cmdlet 會修改本機用戶帳戶。 此 Cmdlet 可以重設本機用戶帳戶的密碼。

注意

Microsoft.PowerShell.LocalAccounts 模組無法在 64 位系統上的 32 位 PowerShell 中使用。

範例

範例 1:變更使用者帳戶的描述

Set-LocalUser -Name "Admin07" -Description "Description of this account."

此命令會變更名為 Admin07 的用戶帳戶描述。

範例 2:變更帳戶的密碼

$Password = Read-Host -AsSecureString
$UserAccount = Get-LocalUser -Name "User02"
$UserAccount | Set-LocalUser -Password $Password

第一個命令會使用 Read-Host Cmdlet 提示您輸入密碼。 命令會將密碼儲存為$Password變數中的安全字串。

第二個命令會使用 Get-LocalUser取得名為 User02 的用戶帳戶。 命令會將帳戶儲存在 $UserAccount 變數中。

第三個命令會在儲存在 $UserAccount的用戶帳戶上設定新密碼。

參數

-AccountExpires

指定用戶帳戶到期的時間。 若要取得 DateTime 物件,請使用 Get-Date Cmdlet。

如果您不想讓帳戶過期,請指定 AccountNeverExpires 參數。

類型:DateTime
Position:Named
預設值:None
必要:False
接受管線輸入:False
接受萬用字元:False

-AccountNeverExpires

表示帳戶未過期。

類型:SwitchParameter
Position:Named
預設值:None
必要:False
接受管線輸入:False
接受萬用字元:False

-Confirm

在執行 Cmdlet 之前,提示您進行確認。

類型:SwitchParameter
別名:cf
Position:Named
預設值:False
必要:False
接受管線輸入:False
接受萬用字元:False

-Description

指定用戶帳戶的批註。 最大長度為 48 個字元。

類型:String
Position:Named
預設值:None
必要:False
接受管線輸入:False
接受萬用字元:False

-FullName

指定用戶帳戶的完整名稱。 完整名稱與用戶帳戶的用戶名稱不同。

類型:String
Position:Named
預設值:None
必要:False
接受管線輸入:False
接受萬用字元:False

-InputObject

指定此 Cmdlet 變更的用戶帳戶。 若要取得用戶帳戶,請使用 Get-LocalUser Cmdlet。

類型:Microsoft.PowerShell.Commands.LocalUser
Position:0
預設值:None
必要:True
接受管線輸入:True
接受萬用字元:False

-Name

指定此 Cmdlet 變更的用戶帳戶名稱。

類型:String
Position:0
預設值:None
必要:True
接受管線輸入:True
接受萬用字元:False

-Password

指定用戶帳戶的密碼。 如果用戶帳戶已連線到Microsoft帳戶,請勿設定密碼。

您可以使用 Read-Host -AsSecureStringGet-CredentialConvertTo-SecureString,為密碼建立 SecureString 物件。

如果您省略 PasswordNoPassword 參數,Set-LocalUser 會提示您輸入使用者的密碼。

類型:SecureString
Position:Named
預設值:None
必要:False
接受管線輸入:False
接受萬用字元:False

-PasswordNeverExpires

指出密碼是否過期。

類型:Boolean
Position:Named
預設值:None
必要:False
接受管線輸入:False
接受萬用字元:False

-SID

指定此 Cmdlet 變更之使用者帳戶的安全性識別碼 (SID)。

類型:SecurityIdentifier
Position:0
預設值:None
必要:True
接受管線輸入:True
接受萬用字元:False

-UserMayChangePassword

指出使用者可以變更用戶帳戶上的密碼。

類型:Boolean
Position:Named
預設值:None
必要:False
接受管線輸入:False
接受萬用字元:False

-WhatIf

顯示 Cmdlet 執行時會發生什麼事。 Cmdlet 未執行。

類型:SwitchParameter
別名:wi
Position:Named
預設值:False
必要:False
接受管線輸入:False
接受萬用字元:False

輸入

System.Management.Automation.SecurityAccountsManager.LocalUser

您可以使用管線將本機用戶傳送至此 Cmdlet。

String

您可以使用管線將字串傳送至此 Cmdlet。

SecurityIdentifier

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

輸出

None

此 Cmdlet 不會傳回任何輸出。

備註

Windows PowerShell 包含下列 Set-LocalUser別名:

  • slu

PrincipalSource 屬性是 localUser LocalGroup及描述物件來源之 LocalPrincipal 物件的 屬性。 可能的來源如下所示:

  • 當地
  • Active Directory
  • Microsoft Entra 群組
  • Microsoft帳戶

只有 Windows 10、Windows Server 2016 和更新版本的 Windows作系統才支援 PrincipalSource。 針對舊版,屬性是空白的。