PowerShell/README.md

187 lines
14 KiB
Markdown
Raw Normal View History

2020-12-14 10:10:01 +01:00
Collection of Useful PowerShell Scripts
=======================================
2020-05-01 11:44:56 +02:00
2021-01-29 11:07:01 +01:00
This repository contains 110+ useful and cross-platform PowerShell scripts - to be used by command-line interface (CLI), for remote control (RC), by context menu, by voice control, by automation software (e.g. Jenkins), or simply to learn PowerShell.
2020-05-01 11:44:56 +02:00
2020-10-03 23:43:59 +02:00
List of Scripts
---------------
2020-12-08 16:13:55 +01:00
The following PowerShell scripts can be found in the [Scripts/](Scripts/) subfolder:
2020-10-03 23:43:59 +02:00
2021-01-28 15:02:16 +01:00
* [add-firewall-rules.ps1](Scripts/add-firewall-rules.ps1) - adds firewall rules for the given executables
* [check-ipv4-address.ps1](Scripts/check-ipv4-address.ps1) - checks the given IPv4 address for validity
* [check-mac-address.ps1](Scripts/check-mac-address.ps1) - checks the given MAC address for validity
* [check-xml-file.ps1](Scripts/check-xml-file.ps1) - checks the given XML file for validity
2020-12-01 12:24:56 +01:00
* [clone-repos.ps1](Scripts/clone-repos.ps1) - clones well-known Git repositories
2021-01-09 15:59:37 +01:00
* [close-calculator.ps1](Scripts/close-calculator.ps1) - closes the calculator program gracefully
* [close-chrome.ps1](Scripts/close-chrome.ps1) - closes Google Chrome gracefully
* [close-edge.ps1](Scripts/close-edge.ps1) - closes Microsoft Edge gracefully
* [close-file-explorer.ps1](Scripts/close-file-explorer.ps1) - closes Microsoft File Explorer gracefully
* [close-program.ps1](Scripts/close-program.ps1) - closes the given program gracefully
* [close-thunderbird.ps1](Scripts/close-thunderbird.ps1) - closes Mozilla Thunderbird gracefully
* [close-windows-terminal.ps1](Scripts/close-windows-terminal.ps1) - closes Windows Terminal gracefully
2020-12-02 08:27:07 +01:00
* [configure-git.ps1](Scripts/configure-git.ps1) - sets up the Git configuration
2020-12-22 16:03:30 +01:00
* [csv-to-text.ps1](Scripts/csv-to-text.ps1) - converts the given CSV file into a text list
* [decrypt-file.ps1](Scripts/decrypt-file.ps1) - encrypts the given file
2021-01-09 12:36:37 +01:00
* [display-time.ps1](Scripts/display-time.ps1) - displays the current time for 10 seconds by default
2020-09-29 20:35:44 +02:00
* [download.ps1](Scripts/download.ps1) - downloads the file/directory from the given URL
2020-10-06 16:23:20 +02:00
* [empty-dir.ps1](Scripts/empty-dir.ps1) - empties the given directory
2020-12-09 14:58:55 +01:00
* [enable-crash-dumps.ps1](Scripts/enable-crash-dumps.ps1) - enables the writing of crash dumps
* [encrypt-file.ps1](Scripts/encrypt-file.ps1) - encrypts the given file
2020-12-30 16:40:07 +01:00
* [generate-qrcode.ps1](Scripts/generate-qrcode.ps1) - generates a QR code
2020-12-08 16:01:57 +01:00
* [inspect-exe.ps1](Scripts/inspect-exe.ps1) - prints basic information of the given executable file
2020-12-22 11:42:32 +01:00
* [list-anagrams.ps1](Scripts/list-anagrams.ps1) - lists all anagrams of the given word
2020-12-30 12:41:38 +01:00
* [list-automatic-variables.ps1](Scripts/list-automatic-variables.ps1) - lists PowerShell automatic variables
2021-01-12 16:42:57 +01:00
* [list-clipboard.ps1](Scripts/list-clipboard.ps1) - lists the contents of the clipboard
2021-01-12 14:48:59 +01:00
* [list-current-timezone.ps1](Scripts/list-current-timezone.ps1) - lists the current time zone details
2021-02-01 07:30:14 +01:00
* [list-environment-variables.ps1](Scripts/list-environment-variables.ps1) - lists all environment variables
2020-10-10 18:18:53 +02:00
* [list-empty-dirs.ps1](Scripts/list-empty-dirs.ps1) - lists empty subfolders in a directory tree
2020-12-30 11:55:01 +01:00
* [list-installed-software.ps1](Scripts/list-installed-software.ps1) - lists the installed software
2020-12-21 15:46:55 +01:00
* [list-files.ps1](Scripts/list-files.ps1) - lists all files in the given folder and also in every subfolder
2021-01-02 12:03:01 +01:00
* [list-formatted.ps1](Scripts/list-formatted.ps1) - lists the current working directory formatted in columns
2021-01-03 11:49:06 +01:00
* [list-fritzbox-calls.ps1](Scripts/list-fritzbox-calls.ps1) - lists the FRITZ!Box calls
2021-01-03 17:27:38 +01:00
* [list-fritzbox-devices.ps1](Scripts/list-fritzbox-devices.ps1) - lists FRITZ!Box's known devices
2020-12-24 10:44:10 +01:00
* [list-logbook.ps1](Scripts/list-logbook.ps1) - lists the content of the logbook
2020-10-10 18:18:53 +02:00
* [list-unused-files.ps1](Scripts/list-unused-files.ps1) - lists unused files in a directory tree
2020-10-10 18:35:22 +02:00
* [list-cmdlets.ps1](Scripts/list-cmdlets.ps1) - lists the PowerShell cmdlets
2020-12-29 15:21:04 +01:00
* [list-earthquakes.ps1](Scripts/list-earthquakes.ps1) - lists earthquakes with magnitude >= 6.0 for the last 30 days
2020-10-10 18:35:22 +02:00
* [list-modules.ps1](Scripts/list-modules.ps1) - lists the PowerShell modules
2020-12-29 15:21:04 +01:00
* [list-news.ps1](Scripts/list-news.ps1) - lists the latest news
2021-01-01 19:27:03 +01:00
* [list-os-releases.ps1](Scripts/list-os-releases.ps1) - lists OS releases and download URL
2021-01-09 15:42:51 +01:00
* [list-printers.ps1](Scripts/list-printers.ps1) - lists all printer known to the computer
2020-12-29 15:37:12 +01:00
* [list-processes.ps1](Scripts/list-processes.ps1) - lists the local computer processes
2020-12-22 09:53:12 +01:00
* [list-random-passwords.ps1](Scripts/list-random-passwords.ps1) - prints a list of random passwords
2020-12-22 10:12:50 +01:00
* [list-random-pins.ps1](Scripts/list-random-pins.ps1) - prints a list of random PIN's
2020-12-29 15:37:12 +01:00
* [list-scripts.ps1](Scripts/list-scripts.ps1) - lists the PowerShell scripts in this repository
2021-01-12 14:48:59 +01:00
* [list-timezones.ps1](Scripts/list-timezones.ps1) - lists all time zones available
2020-12-16 12:03:47 +01:00
* [locate-city.ps1](Scripts/locate-city.ps1) - prints the geographic location of the given city
* [locate-ipaddress.ps1](Scripts/locate-ipaddress.ps1) - prints the geographic location of the given IP address
2020-12-17 08:56:21 +01:00
* [locate-zip-code.ps1](Scripts/locate-zip-code.ps1) - prints the geographic location of the given zip-code
2020-09-25 15:03:48 +02:00
* [MD5.ps1](Scripts/MD5.ps1) - prints the MD5 checksum of the given file
2020-10-02 09:29:24 +02:00
* [make-install.ps1](Scripts/make-install.ps1) - installs built executables and libs to the installation directory
2020-09-25 15:03:48 +02:00
* [moon.ps1](Scripts/moon.ps1) - prints the current moon phase
* [mute-audio.ps1](Scripts/mute-audio.ps1) - mutes audio
2020-12-10 09:54:40 +01:00
* [open-browser.ps1](Scripts/open-browser.ps1) - starts the default Web browser
2021-01-09 12:46:08 +01:00
* [open-calculator.ps1](Scripts/open-calculator.ps1) - starts the calculator program
* [open-email-client.ps1](Scripts/open-browser.ps1) - starts the default email client
2020-12-21 15:25:28 +01:00
* [play-beep.ps1](Scripts/play-beep.ps1) - plays a beep sound
2020-12-14 07:42:56 +01:00
* [poweroff.ps1](Scripts/poweroff.ps1) - halts the local computer (needs administrator rights)
2021-02-03 20:53:56 +01:00
* [query-smart-data.ps1](Scripts/query-smart-data.ps1) - queries the S.M.A.R.T. data of your HDD/SSD's
* [new-email.ps1](Scripts/new-email.ps1) - starts the default email client to write a new email
2021-01-12 17:06:35 +01:00
* [next-joke.ps1](Scripts/next-joke.ps1) - gets the next random Juck Norris joke
2020-12-14 07:42:56 +01:00
* [reboot.ps1](Scripts/reboot.ps1) - reboots the local computer (needs administrator rights)
2021-01-03 11:26:27 +01:00
* [reboot-fritzbox.ps1](Scripts/reboot-fritzbox.ps1) - reboots the FRITZ!box device
2020-12-29 12:03:53 +01:00
* [search-files.ps1](Scripts/search-files.ps1) - searches the given pattern in the given files
2020-11-28 10:00:04 +01:00
* [scan-ports.ps1](Scripts/scan-ports.ps1) - scans the network for open/closed ports
2020-11-28 09:18:36 +01:00
* [send-email.ps1](Scripts/send-email.ps1) - sends an email message
2020-12-09 10:22:41 +01:00
* [send-tcp.ps1](Scripts/send-udp.ps1) - sends a TCP message to the given IP address and port
2020-11-28 09:18:36 +01:00
* [send-udp.ps1](Scripts/send-udp.ps1) - sends a UDP datagram message to the given IP address and port
2020-12-18 14:15:08 +01:00
* [set-timer.ps1](Scripts/set-timer.ps1) - sets a timer for a countdown
2021-01-27 16:21:34 +01:00
* [set-wallpaper.ps1](Scripts/set-wallpaper.ps1) - sets the given image as wallpaper
2020-09-25 15:03:48 +02:00
* [SHA1.ps1](Scripts/SHA1.ps1) - prints the SHA1 checksum of the given file
* [SHA256.ps1](Scripts/SHA256.ps1) - prints the SHA256 checksum of the given file
2020-12-15 15:21:50 +01:00
* [simulate-matrix.ps1](Scripts/simulate-matrix.ps1) - simulates the Matrix (fun)
2020-11-30 16:28:56 +01:00
* [simulate-presence.ps1](Scripts/simulate-presence.ps1) - simulates the human presence against burglars
2021-01-09 16:14:22 +01:00
* [speak-date.ps1](Scripts/speak-date.ps1) - speaks the current date by text-to-speech (TTS)
2021-01-19 09:28:59 +01:00
* [speak-epub.ps1](Scripts/speak-epub.ps1) - speaks the content of the given Epub file by text-to-speech (TTS)
2020-12-08 16:11:01 +01:00
* [speak-file.ps1](Scripts/speak-file.ps1) - speaks the content of the given text file by text-to-speech (TTS)
2020-12-31 11:02:08 +01:00
* [speak-test.ps1](Scripts/speak-test.ps1) - performs a speak test by text-to-speech (TTS)
2020-12-08 16:11:01 +01:00
* [speak-text.ps1](Scripts/speak-text.ps1) - speaks the given text by text-to-speech (TTS)
2021-01-02 13:46:38 +01:00
* [speak-time.ps1](Scripts/speak-time.ps1) - speaks the current time by text-to-speech (TTS)
2020-12-01 10:54:41 +01:00
* [switch-shelly1.ps1](Scripts/switch-shelly1.ps1) - switches a Shelly1 device in the local network
2020-11-22 11:06:31 +01:00
* [take-screenshot.ps1](Scripts/take-screenshot.ps1) - takes a single screenshot
* [take-screenshots.ps1](Scripts/take-screenshots.ps1) - takes multiple screenshots
2020-10-10 18:29:29 +02:00
* [train-dns-cache.ps1](Scripts/train-dns-cache.ps1) - trains the DNS cache with frequently used domain names
2020-12-12 16:18:46 +01:00
* [translate-file.ps1](Scripts/translate-file.ps1) - translates the given file from source to target language
2020-12-08 13:36:22 +01:00
* [translate-text.ps1](Scripts/translate-text.ps1) - translates the given text into other languages
2020-12-29 12:03:53 +01:00
* [turn-volume-up.ps1](Scripts/turn-volume-up.ps1) - turns the audio volume up (+10% by default)
* [turn-volume-down.ps1](Scripts/turn-volume-down.ps1) - turns the audio volume down (-10% by default)
2020-09-25 15:03:48 +02:00
* [txt2wav.ps1](Scripts/txt2wav.ps1) - converts text into a audio .WAV file
* [unmute-audio.ps1](Scripts/unmute-audio.ps1) - unmutes audio
2021-01-20 16:11:38 +01:00
* [update-repos.ps1](Scripts/update-repos.ps1) - updates all Git repositories under the current directory
2021-01-29 11:07:01 +01:00
* [visualize-dir-tree.ps1](Scripts/visualize-dir-tree.ps1) - visualizes the given/current directory tree
2021-01-05 13:55:54 +01:00
* [voice-control.ps1](Scripts/voice-control.ps1) - executes the PowerShell scripts by voice
2020-09-25 15:03:48 +02:00
* [weather.ps1](Scripts/weather.ps1) - prints the current weather forecast
2020-11-11 21:00:51 +01:00
* [weather-alert.ps1](Scripts/weather-alert.ps1) - checks the current weather for critical values
2020-11-11 11:12:18 +01:00
* [weather-report.ps1](Scripts/weather-report.ps1) - prints the local weather report
2020-12-17 08:56:21 +01:00
* [weather-worldwide.ps1](Scripts/weather-worldwide.ps1) - prints the current weather of cities worldwide
2020-09-25 15:03:48 +02:00
* [wakeup.ps1](Scripts/wakeup.ps1) - sends a magic packet to the given computer, waking him up
2020-12-18 16:32:03 +01:00
* [write-big.ps1](Scripts/write-big.ps1) - writes the given text in big letters
2020-12-21 14:42:05 +01:00
* [write-blue.ps1](Scripts/write-blue.ps1) - writes the given text in a blue foreground color
2020-12-22 09:24:30 +01:00
* [write-braille.ps1](Scripts/write-braille.ps1) - writes the given text in Braille
2020-12-21 14:42:05 +01:00
* [write-green.ps1](Scripts/write-green.ps1) - writes the given text in a green foreground color
2020-12-24 10:44:10 +01:00
* [write-logbook.ps1](Scripts/write-logbook.ps1) - writes the given text to the logbook
2021-01-03 18:32:46 +01:00
* [write-marquee.ps1](Scripts/write-marquee.ps1) - writes the given text as marquee
2020-12-21 18:03:36 +01:00
* [write-morse-code.ps1](Scripts/write-morse-code.ps1) - writes the given text in Morse code
2020-12-22 11:42:32 +01:00
* [write-motd.ps1](Scripts/write-motd.ps1) - writes the message of the day (MOTD)
2020-12-21 14:42:05 +01:00
* [write-red.ps1](Scripts/write-red.ps1) - writes the given text in a red foreground color
2020-12-22 11:42:32 +01:00
* [write-rot13.ps1](Scripts/write-rot13.ps1) - encodes or decodes the given text with ROT13
2020-12-21 14:42:05 +01:00
* [write-typewriter.ps1](Scripts/write-typewriter.ps1) - writes the given text with the typewriter effect
2020-12-18 15:40:56 +01:00
* [write-uppercase.ps1](Scripts/write-uppercase.ps1) - writes the given text in uppercase letters
2020-12-18 15:53:17 +01:00
* [write-vertical.ps1](Scripts/write-vertical.ps1) - writes the given text in vertical direction
2020-11-22 10:22:35 +01:00
* [zip-dir.ps1](Scripts/zip-dir.ps1) - creates a zip archive of the given folder
2020-05-22 09:54:16 +02:00
2020-12-08 21:25:56 +01:00
2020-08-11 13:48:27 +02:00
What is PowerShell?
-------------------
2020-05-04 14:39:06 +02:00
PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language.
2020-12-14 08:03:48 +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/
2020-05-01 11:44:56 +02:00
2020-08-11 13:48:27 +02:00
How to get PowerShell?
----------------------
2020-12-12 16:46:57 +01:00
* **Windows 7 and newer** do provide PowerShell by default. It's recommended to install **Windows Terminal** (see the Microsoft Store at https://www.aka.ms/terminal) for a comfortable command-line usage. Per default, Windows does not allow script execution (execution policy "restricted"). To enable this, enter as administrator:
```
$ Set-ExecutionPolicy RemoteSigned
```
2020-12-08 21:40:13 +01:00
* **On CentOS, Debian, Docker, Fedora, macOS, openSUSE, Red Hat, Ubuntu** visit https://github.com/PowerShell/PowerShell for installation.
2020-08-11 14:11:48 +02:00
2020-12-14 10:13:36 +01:00
Afterward, install this collection by clicking on the green "Code" button or execute:
2020-08-11 14:27:11 +02:00
```
$ git clone https://github.com/fleschutz/PowerShell
```
2020-08-11 13:48:27 +02:00
2020-12-08 21:25:56 +01:00
Configure PowerShell as Default Shell
-------------------------------------
* **Linux:** make sure PowerShell is installed, then execute: `chsh -s /snap/bin/powershell <username>`
* **Windows:** no need to, PowerShell is the default shell
2020-06-15 16:01:12 +02:00
2020-06-15 15:56:18 +02:00
2020-12-14 08:03:48 +01:00
Adding 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:
2020-12-08 21:25:56 +01:00
* **Bash or sh:** edit .profile in your home directory and add the line: PATH="$PATH:/path/to/PowerShell/Scripts"
* **Windows:** open the environment variables dialogue and add the full path to Scripts/ to the system environment variable "Path"
2020-08-11 14:11:48 +02:00
2020-06-15 16:01:12 +02:00
2020-12-10 07:11:25 +01:00
Using PowerShell in Context Menus
----------------------------------
2020-12-25 12:03:01 +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)
2020-05-25 20:37:47 +02:00
2020-12-08 21:25:56 +01:00
Script Conventions
------------------
2020-12-29 15:14:21 +01:00
Each PowerShell script should follow the 9 golden rules:
2020-11-30 16:37:02 +01:00
2020-12-14 10:27:57 +01:00
* **Rule #1:** the filename should be named `<verb>-<object>.ps1`
2020-12-16 08:43:35 +01:00
* **Rule #2:** the first line reads `#!/snap/bin/powershell` to support PowerShell on Linux
2020-12-14 10:27:57 +01:00
* **Rule #3:** the script has execute file permissions (chmod a+rx <file>) to support PowerShell on Linux
2020-12-29 12:27:34 +01:00
* **Rule #4:** provide a comment-based help with syntax, description, link, author, and license
2020-12-14 10:27:57 +01:00
* **Rule #5:** prefer command-line options, else ask the user
* **Rule #6:** the encoding of the script is in UTF-8 with BOM to support Unicode characters
2020-12-27 11:20:36 +01:00
* **Rule #7:** use `Set-StrictMode -Version Latest` to enable additional error checking
2020-12-30 11:59:19 +01:00
* **Rule #8:** for readibility use UpperCamelCase for variables and functions, lowerCamelCase for everything else
2020-12-29 15:14:21 +01:00
* **Rule #9:** exits 0 for success, else print an error message and exit with the error code (mostly 1)
2020-05-02 13:01:03 +02:00
2020-06-15 15:09:54 +02:00
Feedback
--------
2020-06-15 16:01:12 +02:00
Send your email feedback to: markus [at] fleschutz [dot] de
2020-06-15 15:09:54 +02:00
2020-05-02 13:01:03 +02:00
License & Copyright
-------------------
This open source release is licensed under the CC0 license. All trademarks are the property of their respective owners.