Common errors and handling of powershell running commands in Windows system

 

Table of contents

 

report error

Error 1 The execution strategy has not been modified

Error 2 Insufficient user rights

Error 3 .ps1 uses Chinese path

Error 4 .ps1 path error


Powershell running instructions, including directly inputting instructions in the terminal and running .ps1 files, frequently appearing error reporting and processing

report error

Error 1 The execution strategy has not been modified

If the execution strategy is not modified, an error will be reported:

.\ReplaceShortcuts.ps1
.\ReplaceShortcuts.ps1 : The file D:\ReplaceShortcuts.ps1 could not be loaded because running scripts is prohibited on this system. For more
information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.
Location line: 1 Characters: 1
+ .\ReplaceShortcuts.ps1
+ ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

 Modification: Before executing .\ReplaceShortcuts.ps1, modify the execution policy.

Set-ExecutionPolicy RemoteSigned

Error 2 Insufficient user rights

If the user permissions are insufficient, an error will be reported:

 Set-ExecutionPolicy RemoteSigned
Set-ExecutionPolicy : Access to registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell'
is denied. To change the execution policy for the default (LocalMachine) scope, start Windows PowerShell with the "Run as administrator" option. To change
the execution policy for the current user, run "Set-ExecutionPolicy -Scope CurrentUser".
Location Line: 1 Characters: 1
+ Set-ExecutionPolicy RemoteSigned
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : PermissionDenied : (:) [Set-ExecutionPolicy], UnauthorizedAccessException
    + FullyQualifiedErrorId : System.UnauthorizedAccessException,
   Microsoft.PowerShell.Commands.SetExecutionPolicyCommand

Method 1: Please use the "Run as administrator" option to start Windows PowerShell. Then "Set-ExecutionPolicy RemoteSigned".

Method 2: Execute "Set-ExecutionPolicy -Scope CurrentUser" instead of being an administrator.

Error 3 .ps1 uses Chinese path

 If the address path in the .ps1 file is set to Chinese, an error may be reported at runtime:

.\ReplaceShortcuts.ps1
Get-ChildItem : Could not find the path "D:\Shortcuts" because it does not exist.
Location D:\Copy\ReplaceShortcuts.ps1:4 Characters: 1
+ Get-ChildItem $shortcutPath -Filter *.lnk | ForEach-Object { + ~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~     + CategoryInfo : ObjectNotFound: (D: Rose: String) [Get-ChildItem], I temNotFoundException     + FullyQualifiedErrorId : PathNotFound, Microsoft.PowerShell.Commands.GetChildItemCommand 



Modify it in [Control Panel], find [Region] in [Control Panel] -> [Management] -> [Change system locale] -> check [Beta version: Use Unicode UTF-8 to provide global language support], As shown below: 

Just restart the computer.

Solution source: PowerShell and cmd commands or Java Runtime.exec to execute cmd commands with Chinese path solution_How to use Chinese path to read files in powershell script_Yanbao's Blog-CSDN Blog

Error 4 .ps1 path error

When executing .\ReplaceShortcuts.ps1

 .\ReplaceShortcuts.ps1
.\ReplaceShortcuts.ps1 : The item '.\ReplaceShortcuts.ps1' was not recognized as the name of a cmdlet, function, script file, or executable. Please check
the spelling of the name and, if a path is included, make sure it is correct and try again.
Location line: 1 Character: 1
+ .\ReplaceShortcuts.ps1
+ ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (.\ReplaceShortcuts.ps1:String ) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Method 1: Use the "cd" command to jump to the folder where ReplaceShortcuts.ps1 is located:

cd D:\path\to\folder

where path\to\folderis the path to the folder you want to access. For example, if you want to access a folder named "Documents" on the D drive, you can use the following command: "cd D:\Documents" This command will change the current working directory to the Documents folder on the D drive, so you can The files and folders in this folder can now be accessed in PowerShell.

Jump to the folder where ReplaceShortcuts.ps1 is located and execute .\ReplaceShortcuts.ps1.

Method 2: Use an absolute path by entering "&" + "absolute path"

& "D:\复制\ReplaceShortcuts.ps1"

Guess you like

Origin blog.csdn.net/weixin_56337147/article/details/130704263