PowerShell/Docs/FAQ.md

132 lines
7.0 KiB
Markdown
Raw Normal View History

2021-04-07 11:03:03 +02:00
PowerShell FAQ
==============
2021-02-11 07:20:18 +01:00
2023-05-10 12:58:03 +02:00
<details>
<summary>What is PowerShell?</summary>
PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language.
</details>
2021-02-11 07:20:18 +01:00
2023-05-10 12:59:02 +02:00
<details>
<summary>Why use PowerShell?</summary>
2022-12-29 20:47:33 +01:00
✔️ It's **powerful** - fully control your computer
2022-09-16 09:20:34 +02:00
2022-12-29 20:51:45 +01:00
✔️ It's **easy to learn** - see the tutorial at: https://www.guru99.com/powershell-tutorial.html
2022-09-16 09:20:34 +02:00
2022-12-29 20:47:33 +01:00
✔️ It's **cross-platform** - available for Linux, Mac OS and Windows
2022-09-16 09:20:34 +02:00
2022-12-29 20:47:33 +01:00
✔️ It's **open-source and free** - see the Github repository at: https://github.com/PowerShell/PowerShell
2022-09-16 09:20:34 +02:00
2022-12-29 20:47:33 +01:00
✔️ It's **fully documented** - see the PowerShell documentation at: https://docs.microsoft.com/en-us/powershell/
2023-05-10 12:59:02 +02:00
</details>
2021-12-22 15:54:05 +01:00
2022-11-05 15:21:01 +01:00
How to install PowerShell?
--------------------------
2022-12-29 21:08:34 +01:00
▶️ **On Windows** it's preinstalled, **but** the script execution policy is *restricted* (forbidden) by default! To change this: open the *Windows PowerShell (Administrator)* console and enter:
2022-12-29 20:47:33 +01:00
```
PS> Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope CurrentUser
```
2022-12-29 21:02:05 +01:00
NOTE: the group policy object (GPO) settings of your organization might disallow changes. In that case contact your system administrator for help.
2022-12-29 20:47:33 +01:00
2022-12-29 21:02:05 +01:00
▶️ **On CentOS, Debian, Docker, Fedora, macOS, openSUSE, Red Hat, Ubuntu** visit https://github.com/PowerShell/PowerShell for installation.
2022-12-29 20:47:33 +01:00
2022-12-29 21:04:12 +01:00
▶️ **On Linux supporting Snaps** execute:
2022-12-29 20:47:33 +01:00
```
$ snap install PowerShell
$ ln -s /snap/bin/pwsh /usr/bin/pwsh
```
2021-02-11 07:39:51 +01:00
2021-11-18 12:13:46 +01:00
How to get the PowerShell Scripts?
----------------------------------
2022-08-29 21:55:17 +02:00
* When using **Git**, execute in a terminal window: `> git clone https://github.com/fleschutz/PowerShell`
2022-08-29 21:58:34 +02:00
* When using **Chocolatey**, execute in *Windows PowerShell (Administrator)*: `> choco install powershell-scripts --version=0.4`
2022-09-04 11:45:40 +02:00
* **Otherwise,** download and unzip it from: https://github.com/fleschutz/PowerShell/archive/master.zip
2021-02-11 07:39:51 +01:00
2021-06-30 19:43:40 +02:00
2022-09-16 09:21:37 +02:00
How to execute PowerShell Scripts?
----------------------------------
2022-09-19 15:27:27 +02:00
1. In the Windows desktop: right-click the script and select: *Execute with PowerShell*
2022-10-28 08:23:44 +02:00
2. In a command-line interface (CLI, e.g. *Windows Terminal*, either local or remote via SSH) by typing: `./<FILENAME_OF_SCRIPT>`.
2022-09-16 09:12:31 +02:00
3. By connecting a context menu item with a script.
4. By voice control, e.g. see repo [talk2windows](https://github.com/fleschutz/talk2windows) for more information.
5. By using automation software, e.g. [Jenkins](https://www.jenkins.io).
2022-09-19 15:29:35 +02:00
6. Automatically on login (see AutoStart folder)/logoff/daily/etc.
2022-09-16 09:12:31 +02:00
2022-08-30 09:24:51 +02:00
What about Security?
--------------------
* Execute scripts only that you trust (and/or where you have checked the code before)!
* Prefer SSH Remoting instead of PowerShell Remoting
* More recommendations by NSA and cyber security centers in the U.S. (CISA), New Zealand (NZ NCSC), and the U.K. (NCSC-UK) can be found here: https://media.defense.gov/2022/Jun/22/2003021689/-1/-1/1/CSI_KEEPING_POWERSHELL_SECURITY_MEASURES_TO_USE_AND_EMBRACE_20220622.PDF
2021-08-30 09:26:11 +02:00
Why do some scripts show gibberish characters?
2021-06-30 19:43:40 +02:00
----------------------------------------------
2022-09-16 09:17:19 +02:00
Those PowerShell scripts are using Unicode characters. 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
2023-04-21 20:26:32 +02:00
How to set PowerShell as favorite 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
2023-04-21 20:18:23 +02:00
How to install a custom PowerShell profile?
-------------------------------------------
2023-04-21 20:24:32 +02:00
Execute: `./set-profile.ps1` in the *Scripts* subfolder, this will install **my-profile.ps1** as your PowerShell profile. It's a nice looking base profile, but can easily be changed to your needs.
2023-04-21 20:18:23 +02: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"
2022-08-29 12:42:59 +02:00
* **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.
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?
-------------------------------------------
2022-05-06 09:05:47 +02:00
* **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.
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
-------------------------------------
2022-12-29 21:19:57 +01:00
Good PowerShell scripts are both user-friendly and platform-independent. I recommend the following rules as a starting point:
2021-04-16 16:43:41 +02:00
2022-09-04 11:30:21 +02:00
1. Use the `<verb>-<object>.ps1` scheme to name the script.
2022-11-26 09:59:24 +01:00
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`.
2022-09-04 11:38:24 +02:00
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>`
2022-09-04 11:30:21 +02:00
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)
2021-02-11 07:20:18 +01:00
2021-08-30 09:19:10 +02:00
2023-04-18 15:41:40 +02:00
Where's the Star History?
-------------------------
[![Star History Chart](https://api.star-history.com/svg?repos=fleschutz/PowerShell&type=Date)](https://star-history.com/#fleschutz/PowerShell&Date)
2021-10-01 11:55:01 +02:00
Want to contribute or 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-10-01 11:55:01 +02:00
Or even better: Fork the repository, add or fix the script 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
-----------------------------------
2023-04-16 10:38:33 +02:00
Send your question to: markus.fleschutz [at] gmail.com