2021-02-11 07:30:40 +01:00
PowerShell Scripts FAQ
======================
2021-02-11 07:20:18 +01:00
What is PowerShell?
-------------------
PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language.
2021-03-29 12:40:02 +02:00
Why use PowerShell?
-------------------
2021-02-11 07:20:18 +01:00
* **it's powerful**: fully control your computer
* **it's cross-platform**: available for Linux, Mac OS and Windows
* **it's open-source**: see the Github repository at https://github.com/PowerShell/PowerShell
* **it's easy to learn**: see the tutorial at https://www.guru99.com/powershell-tutorial.html
* **it's fully documented**: see the official docs at https://docs.microsoft.com/en-us/powershell/
How to get PowerShell?
----------------------
2021-02-16 10:07:49 +01:00
* **Windows 7 and newer** do provide PowerShell by default. However, script execution is **not allowed by default** (execution policy is "restricted"). To enable this, enter as administrator:
2021-02-11 07:20:18 +01:00
```
$ Set-ExecutionPolicy RemoteSigned
```
2021-02-11 13:44:08 +01:00
* **Want to use Snaps?** Install the PowerShell snap by executing:
2021-02-11 07:39:51 +01:00
```
$ snap install PowerShell
2021-02-11 17:08:14 +01:00
$ ln -s /snap/bin/powershell /bin/powershell
2021-02-11 07:39:51 +01:00
```
2021-02-11 07:52:12 +01:00
* **On CentOS, Debian, Docker, Fedora, macOS, openSUSE, Red Hat, Ubuntu** visit https://github.com/PowerShell/PowerShell for installation.
2021-02-11 07:39:51 +01:00
How to get the PowerShell Scripts?
----------------------------------
2021-02-11 07:44:14 +01:00
Git users do execute:
2021-02-11 07:20:18 +01:00
```
$ git clone https://github.com/fleschutz/PowerShell
```
2021-02-11 07:44:14 +01:00
otherwise simply download it from:
2021-02-11 07:39:51 +01:00
https://github.com/fleschutz/PowerShell/archive/master.zip
2021-02-11 07:30:40 +01:00
How to Configure PowerShell as Default Shell?
---------------------------------------------
2021-03-29 11:33:47 +02:00
The default shell on Windows is PowerShell, on Linux it's mostly Bash.
2021-03-29 11:52:30 +02:00
* **On Linux:** make sure PowerShell is installed, then execute: `chsh -s /bin/powershell <username>`
* **On Windows:** no need to, PowerShell is the default shell
2021-02-11 07:20:18 +01:00
2021-02-11 07:30:40 +01:00
How to add the Scripts to the Search Path?
------------------------------------------
2021-02-11 07:20:18 +01:00
Want to use the PowerShell scripts everywhere on the command-line? Then you need to add the Scripts/ subfolder to the search path:
2021-03-29 11:52:30 +02:00
* **On Linux using Bash:** edit .profile in your home directory and add the line: PATH="$PATH:/path/to/PowerShell/Scripts"
* **On Windows:** open the environment variables dialogue and add the full path to Scripts/ to the system environment variable "Path"
2021-02-11 07:20:18 +01:00
2021-02-11 07:30:40 +01:00
How to use PowerShell in Context Menus?
---------------------------------------
2021-02-11 07:20:18 +01:00
* to enable "right-click > New > Windows PowerShell Script" execute `Add_ps1_to_New_context_menu.reg` in subfolder [Misc/ ](Misc )
* to disable this execute `Remove_ps1_from_New_context_menu.reg` in subfolder [Misc/ ](Misc )
2021-02-11 10:45:31 +01:00
How to edit PowerShell Scripts?
-------------------------------
2021-03-29 11:51:06 +02:00
* use PowerShell ISE (Integrated Scripting Environment) on Windows to write, test and debug scripts
* use Visual Studio Code (available for free on Linux, Mac OS and Windows) - supporting syntax highlighting, on-the-fly problem checking and an integrated PowerShell Console.
* or simply use your favorite text editor
2021-02-11 10:45:31 +01:00
2021-02-15 12:09:36 +01:00
How to write good PowerShell Scripts?
2021-02-11 07:30:40 +01:00
-------------------------------------
2021-02-15 12:08:50 +01:00
Each PowerShell script should follow the 10 golden rules:
2021-02-11 07:20:18 +01:00
1. the filename should be named `<verb>-<object>.ps1`
2021-02-16 10:03:20 +01:00
2. the first line reads `#!/bin/powershell` (to support Linux)
3. the script has execute file permissions: chmod a+rx < file > (to support Linux)
2021-02-11 07:20:18 +01:00
4. provide a comment-based help with syntax, description, link, author, and license
5. check the requirements (e.g. #Requires -RunAsAdministrator, or #Requires -Version 3)
6. prefer command-line options, else ask the user
2021-02-16 10:03:20 +01:00
7. recommended is `Set-StrictMode -Version Latest` to enable additional error checking
2021-02-11 07:20:18 +01:00
8. for readibility use UpperCamelCase for variables and functions, lowerCamelCase for everything else
2021-02-16 10:03:20 +01:00
9. on error call write-error with keyword "ERROR:" (to support log parsers) and exit the error code (mostly 1)
10. on success exit with error code 0 (exit 0)
2021-02-11 07:20:18 +01:00
2021-02-15 12:08:50 +01:00
Your Question is not answered here?
-----------------------------------
2021-02-11 07:30:40 +01:00
Send your question to: markus [at] fleschutz [dot] de