PowerShell/Docs/FAQ.md
Markus Fleschutz 50dfa4f876
Update FAQ.md
2022-12-29 21:04:12 +01:00

6.4 KiB

PowerShell FAQ

What is PowerShell?

PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language.

Why use PowerShell?

✔️ It's powerful - fully control your computer

✔️ It's easy to learn - see the tutorial at: https://www.guru99.com/powershell-tutorial.html

✔️ It's cross-platform - available for Linux, Mac OS and Windows

✔️ It's open-source and free - see the Github repository at: https://github.com/PowerShell/PowerShell

✔️ It's fully documented - see the PowerShell documentation at: https://docs.microsoft.com/en-us/powershell/

How to install PowerShell?

▶️ On Windows it's preinstalled, but the script execution policy is by default restricted (forbidden)! To change this: open the Windows PowerShell (Administrator) console and enter:

 PS> Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope CurrentUser

NOTE: the group policy object (GPO) settings of your organization might disallow changes. In that case contact your system administrator for help.

▶️ On CentOS, Debian, Docker, Fedora, macOS, openSUSE, Red Hat, Ubuntu visit https://github.com/PowerShell/PowerShell for installation.

▶️ On Linux supporting Snaps execute:

 $ snap install PowerShell
 $ ln -s /snap/bin/pwsh /usr/bin/pwsh

How to get the PowerShell Scripts?

  • When using Git, execute in a terminal window: > git clone https://github.com/fleschutz/PowerShell
  • When using Chocolatey, execute in Windows PowerShell (Administrator): > choco install powershell-scripts --version=0.4
  • Otherwise, download and unzip it from: https://github.com/fleschutz/PowerShell/archive/master.zip

How to execute PowerShell Scripts?

  1. In the Windows desktop: right-click the script and select: Execute with PowerShell
  2. In a command-line interface (CLI, e.g. Windows Terminal, either local or remote via SSH) by typing: ./<FILENAME_OF_SCRIPT>.
  3. By connecting a context menu item with a script.
  4. By voice control, e.g. see repo talk2windows for more information.
  5. By using automation software, e.g. Jenkins.
  6. Automatically on login (see AutoStart folder)/logoff/daily/etc.

What about Security?

Why do some scripts show gibberish characters?

Those PowerShell scripts are using Unicode characters. Use a modern console supporting UTF-8 such as Windows Terminal, please.

How to set PowerShell as default shell on Linux?

Make sure PowerShell is installed, then execute: chsh -s /usr/bin/pwsh <username>. In case you experience an "invalid shell" error, add "/usr/bin/pwsh" to /etc/shells.

How to add the scripts to the search path?

Want to use the PowerShell scripts everywhere on the command-line? Then you need to add the Scripts/ subfolder to the search path:

  • On Linux using Bash: edit .profile in your home directory and add the line: PATH="$PATH:/path/to/PowerShell/Scripts"
  • On Windows: open Settings > System > About > Advanced system settings > Environment Variables, edit the user's variable "Path", and add the full path to the Scripts/ directory.

How to use PowerShell in Windows context menus?

  • to enable "right-click > New > Windows PowerShell Script" execute Add_ps1_to_New_context_menu.reg in subfolder Data/
  • to disable this execute Remove_ps1_from_New_context_menu.reg in subfolder Data/

Which editor to use for PowerShell scripts?

  • Visual Studio Code - Supporting syntax highlighting, on-the-fly problem checking and an integrated PowerShell Console (available for free on Linux, Mac OS and Windows, now recommended by Microsoft).
  • PowerShell ISE (Integrated Scripting Environment) - Former official PowerShell development environment included with Microsoft Windows.
  • PowerShell Studio - Powerful PowerShell IDE with module, help, and user interface development tools, high DPI support and regular updates.
  • PowerShell Plus - All in one IDE.
  • Atom package - Add-on with PowerShell language support for Atom.
  • SublimeText package - Add-on with PowerShell language support for Sublime Text.
  • or your favorite text editor as an alternative.

How to write good PowerShell scripts?

Good PowerShell scripts are user-friendly and platform-independent. The following rules should be a good starting point:

  1. Use the <verb>-<object>.ps1 scheme to name the script.
  2. Use UTF-8 BOM encoding to support Unicode characters in the script.
  3. Add a comment-based help at the beginning with: .SYNOPSIS, .DESCRIPTION, .PARAMETER, .EXAMPLE, .LINK, and .NOTES.
  4. Check the requirements for the script, e.g. #Requires -RunAsAdministrator, or #Requires -Version 3
  5. Prefer command-line options, else ask the user for help
  6. Recommended is Set-StrictMode -Version Latest to enable additional error checking.
  7. For readibility use UpperCamelCase for variables and functions, lowerCamelCase for everything else.
  8. Set execute file permissions for Linux: chmod a+rx <filename>
  9. On success exit with error code 0 (exit 0), otherwise print the error with keyword "ERROR:" (to support log parsers) and exit the error code (mostly 1)

Want to contribute or found a bug or issue?

If you find something bad (like a bug, error, or any issue), please report it here by open an Issue.

Or even better: Fork the repository, add or fix the script and submit a pull request, so others can participate too.

Your question is not answered here?

Send your question to: markus [at] fleschutz [dot] de