PowerShell/Docs/FAQ.md

96 lines
4.8 KiB
Markdown
Raw Normal View History

2021-04-07 11:03:03 +02:00
PowerShell 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
2021-04-07 11:06:16 +02:00
* **it's open-source and free**: see the Github repository at https://github.com/PowerShell/PowerShell
2021-02-11 07:20:18 +01:00
* **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-08-30 09:29:47 +02:00
* **On Windows 7 and newer** PowerShell is provided by default. However, script execution is **disallowed by default** (execution policy is "restricted"). To enable this, enter as administrator:
2021-02-11 07:20:18 +01:00
```
2021-08-30 09:22:10 +02:00
PS> Set-ExecutionPolicy RemoteSigned
2021-02-11 07:20:18 +01:00
```
2021-04-07 11:19:16 +02:00
* **On CentOS, Debian, Docker, Fedora, macOS, openSUSE, Red Hat, Ubuntu** visit https://github.com/PowerShell/PowerShell for installation.
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-04-07 11:36:19 +02:00
$ ln -s /snap/bin/pwsh /usr/bin/pwsh
2021-02-11 07:39:51 +01:00
```
2021-08-30 09:26:11 +02:00
How to get the collection of PowerShell scripts?
------------------------------------------------
2021-04-07 11:19:16 +02:00
* Git users execute: `$ git clone https://github.com/fleschutz/PowerShell`
2021-08-30 09:19:10 +02:00
* otherwise download & unzip: https://github.com/fleschutz/PowerShell/archive/master.zip
2021-02-11 07:39:51 +01:00
2021-06-30 19:43:40 +02:00
2021-08-30 09:26:11 +02:00
Why do some scripts show gibberish characters?
2021-06-30 19:43:40 +02:00
----------------------------------------------
2021-08-30 09:29:47 +02:00
Those PowerShell scripts are using Unicode input & output. Use a modern console supporting UTF-8 such as Windows Terminal, please.
2021-03-29 11:33:47 +02:00
2021-04-20 10:59:03 +02:00
2021-08-30 09:26:11 +02:00
How to set PowerShell as default shell on Linux?
------------------------------------------------
2021-04-20 10:59:03 +02:00
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.
2021-02-11 07:20:18 +01:00
2021-08-30 09:26:11 +02:00
How to add the scripts to the search path?
2021-02-11 07:30:40 +01:00
------------------------------------------
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-08-30 09:29:47 +02:00
How to use PowerShell in Windows context menus?
-----------------------------------------------
2021-08-29 12:20:37 +02:00
* to enable "right-click > New > Windows PowerShell Script" execute `Add_ps1_to_New_context_menu.reg` in subfolder [Data/](../Data)
* to disable this execute `Remove_ps1_from_New_context_menu.reg` in subfolder [Data/](../Data)
2021-02-11 07:20:18 +01:00
2021-02-11 10:45:31 +01:00
2021-08-30 10:34:53 +02:00
Which editor to use for PowerShell scripts?
-------------------------------------------
* *PowerShell ISE* (Integrated Scripting Environment) - Official PowerShell development environment included with Microsoft Windows.
* *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).
* *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.
2021-04-07 11:19:16 +02:00
* or simply use your *favorite text editor*
2021-02-11 10:45:31 +01:00
2021-08-30 09:19:10 +02:00
2021-08-30 09:26:11 +02:00
How to write good PowerShell scripts?
2021-02-11 07:30:40 +01:00
-------------------------------------
2021-04-20 10:48:40 +02:00
Good PowerShell scripts are user-friendly and platform-independant. As a guideline follow these rules, please:
2021-04-16 16:43:41 +02:00
1. the filename is named using the `<verb>-<object>.ps1` scheme
2021-08-30 09:19:10 +02:00
2. the encoding is UTF-8-BOM to support and use Unicode characters
2021-04-16 16:43:41 +02:00
3. the script has execute file permissions: chmod a+rx <file> (for Linux support)
2021-04-21 19:53:52 +02: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 for help
7. recommended is `Set-StrictMode -Version Latest` to enable additional error checking
8. for readibility use UpperCamelCase for variables and functions, lowerCamelCase for everything else
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-08-30 09:19:10 +02:00
2021-08-30 09:26:11 +02:00
Found a bug or issue?
2021-08-30 09:19:10 +02:00
---------------------
If you find something bad (like a bug, error, or any issue), please report it here by open an Issue.
2021-08-30 09:29:47 +02:00
Or even better: Fork the Repository, fix it and submit a pull request, so others can participate too.
2021-08-30 09:19:10 +02:00
2021-08-30 09:26:11 +02:00
Your question is not answered here?
2021-02-15 12:08:50 +01:00
-----------------------------------
2021-02-11 07:30:40 +01:00
Send your question to: markus [at] fleschutz [dot] de