Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PowerShell Getting Started Guide

Getting started with PowerShell

PowerShell is a command-line shell and scripting environment built by Microsoft for Windows, mainly used for system administration and automation tasks.

in Windows 11 , simply search for PowerShell in the Start menu.

Common shortcuts include (for 64-bit systems):

Shortcut name Description
Windows PowerShell 64-bit command-line version
Windows PowerShell ISE 64-bit graphical script editor
Windows PowerShell (x86) 32-bit command-line version
Windows PowerShell ISE (x86) 32-bit graphical script editor

Note: Windows 11 does not support 32-bit systems, but still includes an x86 version of PowerShell for compatibility with old programs.


Ways to launch PowerShell

Normal launch method

Click the Windows PowerShell shortcut to launch the PowerShell console.

The PowerShell console title bar shows "Windows PowerShell".

**Note:** Some commands run normally when PowerShell is launched as a normal user, but PowerShell itself does not participate in User Account Control (UAC) and cannot prompt the user for elevation.

User Account Control (UAC) explained

**UAC purpose:** A Windows security feature that prevents malicious code from running with elevated privileges.

**Normal user permission limits:** When running as a normal user, attempting to run commands requiring administrator privileges will result in an error.

For example, stopping a Windows service:



Stop-Service -Name W32Time

Example error message:



Stop-Service : Service 'Windows Time (W32Time)' cannot be stopped due to

the following error: Cannot open W32Time service on computer '.'.

At line:1 char:1

+ Stop-Service -Name W32Time

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : CloseError: (System.ServiceProcess.ServiceCon

   troller:ServiceController) [Stop-Service], ServiceCommandException

    + FullyQualifiedErrorId : CouldNotStopService,Microsoft.PowerShell.Comm

   ands.StopServiceCommand

Solution: You need to

run PowerShell as a user elevated to local administrator privileges.

A second domain user account is configured, following the principle of least privilege: it is not a domain admin and has no in-domain elevation rights.

Steps:

**System prompt:** Because you are currently logged in as a normal user, Windows will prompt for local administrator credentials.

**Confirm elevation:** The elevated PowerShell window title bar shows "Administrator: Windows PowerShell".

**Effect:** Running commands that require administrator privileges in the elevated PowerShell no longer triggers UAC errors.

Principle of privilege elevation: Only elevate PowerShell as administrator when absolutely necessary.

**Remote computers:** When working with remote computers, there is no need to elevate PowerShell privileges. Elevation only affects local commands.

**Pin shortcut:** You can pin the PowerShell or Windows Terminal shortcut to the taskbar for easy launching:

**Safely launching elevated PowerShell:** If you need to launch PowerShell with elevated privileges, hold the Shift key while right-clicking the pinned PowerShell icon on the taskbar and select "Run as administrator"


Guide to Determining PowerShell Version and Execution Policy

I. Checking the PowerShell Version

PowerShell provides the automatic variable $PSVersionTable, which contains the version and related information of the current PowerShell session.

$PSVersionTable

The output is similar to the following:

Name                           Value

----                           -----

PSVersion                      5.1.22621.2428

PSEdition                      Desktop

PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}

BuildVersion                   10.0.22621.2428

CLRVersion                     4.0.30319.42000

WSManStackVersion              3.0

PSRemotingProtocolVersion      2.3

SerializationVersion           1.1.0.1

Version notes

II. Understanding the Execution Policy

The execution policy is a PowerShell security feature that controls the conditions under which scripts run, preventing the accidental execution of malicious scripts.

Note: The execution policy is not a security boundary; experienced users can bypass it.

Scope of the execution policy

Common Windows default execution policies

Operating system version Default execution policy
Windows Server 2022 RemoteSigned
Windows Server 2019 RemoteSigned
Windows Server 2016 RemoteSigned
Windows 11 Restricted
Windows 10 Restricted

Restricted indicates that no scripts are allowed to run by default.

III. Querying the Current Execution Policy

Query the current policy:

Get-ExecutionPolicy

The output is similar to the following:

Restricted 

List execution policy settings for all scopes:

Get-ExecutionPolicy -List

The output is similar to the following:

Scope ExecutionPolicy

        ----- ---------------

MachinePolicy       Undefined

   UserPolicy       Undefined

      Process       Undefined

  CurrentUser       Undefined

 LocalMachine       Undefined

IV. Example of How the Execution Policy Affects Script Execution

Suppose there is a script file Get-TimeService.ps1; running this command directly in the interactive command line works normally:



Get-Service -Name W32Time

However, when running the same command from a script, PowerShell will return an error.



.\Get-TimeService.ps1

Error message:

.\Get-TimeService.ps1 : File C:\tmp\Get-TimeService.ps1 cannot be loaded

because running scripts is disabled on this system. For more information,

see about_Execution_Policies at

https:/go.microsoft.com/fwlink/?LinkID=135170.

At line:1 char:1

+ .\Get-TimeService.ps1

+ ~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : SecurityError: (:) [], PSSecurityException

    + FullyQualifiedErrorId : UnauthorizedAccess

V. Modifying the Execution Policy

1. Set to RemoteSigned (recommended)

Allows running local scripts and remote scripts signed by a trusted publisher.

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

Do you want to change the execution policy? [Y] Yes  [A] Yes to All  ...

2. Modify only the current user's execution policy (no administrator privileges needed)

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

VI. Common Error Messages and Solutions

Error when modifying the LocalMachine policy without elevation:

Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' is denied.

Solution: Run PowerShell as administrator and retry, or modify only the current user policy.

VII. Verification After Modification

After the execution policy is changed to RemoteSigned , run the script:

.\Get-TimeService.ps1

Output result:

Status   Name    DisplayName

------   ----    -----------

Running  W32Time Windows Time

VIII. Precautions