To execute a PowerShell command, open PowerShell (as Administrator if required), type the command, and press Enter. PowerShell is built into Windows, so no extra software is needed, but you must have appropriate permissions and know the correct syntax.
What You'll Need
- A Windows PC (PowerShell is included by default in Windows 7 SP1 and later, including Windows 10 and 11)
- Administrator privileges if the command modifies system settings, installs software, or writes to protected locations (e.g., Program Files, Registry keys, or system services)
- Basic familiarity with command-line concepts (optional but helpful)
Step-by-Step
- Open PowerShell: Press Windows + X to open the Quick Access menu, then select Windows PowerShell or Windows PowerShell (Admin). If you need to run commands that change system-wide settings (e.g., modifying services, registry, or installing features), choose Windows PowerShell (Admin) and confirm any User Account Control (UAC) prompt by clicking Yes.
- Wait for the prompt: You’ll see a window with a path like
PS C:\Users\YourName>. This is the PowerShell command prompt. Do not close it until the command finishes executing. - Type the command: Enter the exact PowerShell command you want to run. For example, to list all running services, type
Get-Service | Where-Object Status -eq 'Running'and press Enter. PowerShell is case-insensitive, but use correct spelling and punctuation (e.g., hyphens, quotes, parentheses). - Press Enter to execute: After typing the command, press Enter. PowerShell processes the command and displays output (if any) in the same window. Some commands return objects; others show text or error messages.
- Run multiple commands (optional): To chain commands, separate them with a semicolon (
;). For instance,Get-Process | Sort-Object CPU -Descending; Get-Datefirst lists processes by CPU usage, then shows the current date and time. - Cancel or stop a running command: If a command appears stuck or is taking too long, press Ctrl + C to interrupt it safely.
Common Mistakes
- Running a command without Administrator rights when elevated privileges are required—this causes
Access DeniedorPermissionDeniederrors. - Using incorrect syntax: PowerShell uses verb-noun pairs (e.g.,
Get-Process, notGetProcessorGet-Process.exe). - Forgetting quotes around strings with spaces:
Start-Process "notepad.exe"works, butStart-Process notepad.exemay fail if the path contains spaces (e.g.,Start-Process "C:\Program Files\App\app.exe"). - Confusing cmd.exe commands with PowerShell: Commands like
dirorclswork in PowerShell because they’re aliases, but others (e.g.,ipconfig) require calling the executable explicitly:& ipconfig. - Ignoring error messages: PowerShell errors often include helpful suggestions (e.g.,
Run 'Get-Command -Name Set-ExecutionPolicy'), but users may scroll past them too quickly.
Tips
- Use tab completion: Type part of a command (e.g.,
Get-P) and press Tab to cycle through matching cmdlets likeGet-Process,Get-PSDrive, etc. - Get help: Type
Get-Help <command>(e.g.,Get-Help Get-Process -Detailed) for usage examples, parameter descriptions, and syntax. - Check execution policy: If you see
script execution is disabled on this system, runSet-ExecutionPolicy RemoteSigned -Scope CurrentUser(requires no Admin rights for your user) to allow local scripts. AvoidUnrestrictedunless absolutely necessary. - Use parentheses for subexpressions:
Write-Host "Today is $((Get-Date).DayOfWeek)"embeds a command inside a string correctly. - Save useful commands in a script file: Create a
.ps1file (e.g.,backup.ps1) and run it with.ackup.ps1from the correct directory (ensure execution policy allows it). - Run commands remotely: Use
Invoke-Command -ComputerName <PCName> -ScriptBlock { <command> }for remote execution (requires WinRM service enabled and configured).
Troubleshooting
If your command fails, follow these steps in order:
- Check for typos: Re-type the command carefully. PowerShell cmdlets are case-insensitive but spelling-sensitive—
get-serviceworks, butget-srvicedoes not. - Run as Administrator: Close PowerShell, reopen it as Admin (via Windows + X menu), then re-run the command. This fixes most
Access Deniederrors. - Verify the command exists: Run
Get-Command <cmdlet-name>. If nothing returns, the cmdlet may be part of a module not loaded. TryImport-Module <ModuleName>(e.g.,Import-Module ActiveDirectory). - Check the execution policy: Run
Get-ExecutionPolicy. If it returnsRestrictedorAllSigned, change it for your user:Set-ExecutionPolicy RemoteSigned -Scope CurrentUser. - Review the error message: Look at the
CategoryInfoandFullyQualifiedErrorIdlines. For example,File C:\script.ps1 cannot be loaded because running scripts is disabledindicates an execution policy issue. - Test with a simple command: Try
Write-Host "Hello"orGet-Date. If these fail, PowerShell itself may be corrupted—restart your PC or repair Windows viasfc /scannowin Command Prompt (run as Admin). - Check for typos in paths or arguments: If using paths with spaces, wrap them in double quotes. Example:
Start-Process -FilePath "C:\My Folder\app.exe".
Frequently Asked Questions
Can I run PowerShell commands from the regular Command Prompt?
Yes. In Command Prompt (cmd.exe), type powershell -Command "<your-command>". For example: powershell -Command "Get-Process". This launches PowerShell, runs the command, and returns output to the Command Prompt window.
Why does my PowerShell window close immediately after running a script?
This happens when you double-click a .ps1 file or run it from File Explorer. To keep the window open, launch PowerShell first, then run the script from within it (e.g., .ackup.ps1). Alternatively, add Read-Host -Prompt "Press Enter to exit" at the end of your script.
How do I run a one-line PowerShell command without opening the full GUI?
Use the powershell executable with the -Command or -EncodedCommand parameter. For example, create a shortcut with target: powershell.exe -Command "Get-Service | Export-Csv services.csv". Or use Windows + R, type powershell -NoExit -Command "Get-Process", and press Enter.
Is there a way to undo a PowerShell command?
PowerShell itself has no built-in undo for most commands. However, many cmdlets support the -WhatIf parameter to preview changes before applying them (e.g., Remove-Item "test.txt" -WhatIf). Always test risky commands with -WhatIf first when supported.
Can I run PowerShell commands on macOS or Linux?
Yes, but only with PowerShell Core (now called PowerShell 6.0+), a cross-platform version available from Microsoft. Download it from the official PowerShell GitHub releases page. The syntax is nearly identical, though some Windows-specific cmdlets (e.g., Get-WmiObject) may not work as expected.
What does the red text in PowerShell mean?
Red text indicates an error or warning. Non-terminating errors (e.g., a single file not found in a bulk operation) show red text but let the command continue. Terminating errors (e.g., syntax failure) stop execution and always appear in red. Use $Error[0] to inspect the last error object for details.