PowerShell命令与Bash脚本命令组合后无法执行问题咨询
Got it, let's break down why your combined script isn't working and walk through the solutions.
The Root Cause
Your first two commands (Net user ...) are CMD-native commands, while the third one (Set-LocalUser ...) is a PowerShell-exclusive cmdlet. If you're running all three in a .bat/.cmd script (or directly in a CMD window), the CMD interpreter doesn't understand PowerShell syntax—so it'll throw an error when it hits the Set-LocalUser line.
Solution 1: Wrap PowerShell Commands in CMD Scripts
If you want to keep using a CMD script, you can call PowerShell explicitly to run the third command. Use the powershell -Command parameter to pass the PowerShell cmdlet to the PowerShell interpreter:
Net user administrator /active:yes Net user administrator Hardware123 powershell -Command "Set-LocalUser -Name 'administrator' -PasswordNeverExpires 1"
Note: We use single quotes inside the double quotes to avoid syntax conflicts in CMD.
Solution 2: Convert Everything to a PowerShell Script (Recommended)
For cleaner, more native execution, rewrite all commands using PowerShell's built-in local user management cmdlets. This avoids mixing interpreters entirely:
# Enable the administrator account Enable-LocalUser -Name "administrator" # Set the administrator password (convert plain text to secure string first) $securePassword = ConvertTo-SecureString "Hardware123" -AsPlainText -Force Set-LocalUser -Name "administrator" -Password $securePassword # Set password to never expire Set-LocalUser -Name "administrator" -PasswordNeverExpires $true
Save this as a .ps1 file, then right-click it and select "Run with PowerShell" (make sure you're running as an administrator).
Critical Note
All these commands require administrator privileges to run successfully. Whether you're using CMD or PowerShell, always launch the interpreter by right-clicking and selecting "Run as administrator"—otherwise you'll get permission denied errors.
内容的提问来源于stack exchange,提问作者Ar.Bazargani

