Awesome Open Source
Awesome Open Source

PowerShell Scripts

These scripts are my custom profile scripts for PS command windows and for ISE including a custom "Dark Selenitic" theme for ISE.

How to install

This entire repo can be overlayed ontop of your Documents\WindowsPowerShell folder.

Set-Location $env:USERPROFILE\Documents;
git clone https://github.com/stevencohn/WindowsPowerShell.git

If downloading the repo as a Zip file then you'll need to unblock all files:

Get-ChildItem -Path "${env:USERPROFILE}\Documents\WindowsPowerShell" -Recurse | Unblock-File

To download just the Initialize-Machine.ps1 script:

[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12';
Invoke-WebRequest -Uri 'https://raw.githubusercontent.com/stevencohn/WindowsPowerShell/main/Initialize-Machine.ps1' -OutFile C:\Initialize-Machine.ps1

Loosen up the execution policy. Consider signing the scripts and setting the execution policy to AllSigned.

Set-ExecutionPolicy RemoteSigned -Force -Confirm:$false;
Set-Executionpolicy -Scope CurrentUser -ExecutionPolicy UnRestricted -Force -Confirm:$false

See also the Install-Programs.ps1 script below.

Commands

These scripts are located in the Modules\Scripts folder.

Clear-Events [-Quiet]

Clear all events from the Windows Event Log.

Clear-Temp [-Quiet]

Clear the contents of TEMP, Windows\TEMP, and LocalAppData\TEMP.

ConvertFrom-BinHex -Hex v -Unicode

Convert a BinHex encoded string back to its original string value.

ConvertFrom-Hex -Hex v

Convert a Hex string into an integer value. If the string contains six or eight characters then it is also interpreted as an ARGB value and each component part is displayed.

ConvertTo-Hex -R r [-G g] [-B b]

Convert integer values to a Hex string. If one integer is specified then it is converted. If three integers are specified then they are assumed to be RGB values and combined into a single Hex string.

ConvertTo-mp3 -InputPath p [-Bitrate r] [-FullQuality] [-Info] [-Yes]

Primarily used to convert .m4a audio files to .mp3

Copy-Console [-OutFile f] [-All] [-Rtf] [-Trim]

Copy the contents of the Powershell console window preserving color. Can be sent to an out file or pasted into Word or OneNote.

Copy-Playlist -Playlist p -Target t -WhatIf

Copies all music files referenced by the given playlist to the specified location. For example, this can be used to copy music in an .m3u playlist file to a USB thumbdrive.

Edit-Hosts

Open the hosts file in Notepad.

Edit-Playlist -Replace r [-Path p] [-Type t]

Replace the path of all items in a playlist file to the current user's MyMusic path.

Edit-PSProfile

Run VSCode with ~Documents\WindowsPowerShell as root folder. Aliased to ep

Enable-PersonalOneDrive

Enable Personal OneDrive sync when both Business and Personal accounts are registered on the local machine. Will indicate when either is account not available.

Enable-TrustedRemoting

Enable PowerShell remoting and trusted hosts for the current machine, typically run on a VM that will be used for automated tasks such as CI/CD.

Get-Account -Username u [-Domain d] [-SID]

Report the account information for the given username and optionally a specified domain.

alt text

Get-ChildItemColorized -Dir d [-la]

Display a colorized directory listing along with total size. Aliased to ls

alt text

Get-Colors [-All] [-Cmd] [-PS] [-ConEmu] [-Script]

Display the console colors for Command console, PowerShell console, and ConEmu consoles.

alt text

Get-CommandLine [-Name n] [-Only] [-ReturnValue] [-ShowSystem]

Report processes with their command lines, highlighting an optional search string

Get-DirSize -Dir d [-la]

Report the size of all items in the specified folder. Used as a sub-routine of Get-ChildItemColorized.

Get-DotNetVersion

Get the versions of.NET Framework installations on the local computer.

Get-Env [-Name n] [-Value v]

Report environment variables in colorized categoties with optional search highlighting.

alt text

Get-Hosts

Display the /etc/hosts file, colorized.

Get-Installed [-Outfile f]

Report all installed applications registered on the local system.

Get-Network [-Preferred] [-Addresses] [-Verbose]

Determines the most likely candidate for the active Internet-specific network adapter on this machine. All other adpaters such as tunneling and loopbacks are ignored. Only connected IP adapters are considered. Wifi aliases are shown.

alt text

Get-Merges -Project p [-Branch b] [-Since yyyy-mm-dd] [-Last n] [-Raw]

Reports all merges for the given git repo after a specified date or within the last n days.

Get-Path [-Search s] [-Sort] [-Verbose]

Display the PATH environment variable as a list of strings rather than a single string and displays the source of each value defined in the Registry: Machine, User, or Process. Verbose mode dumps the User and System Paths as stored in the Windows Registry.

alt text

Get-Performance

Get and report performance metrics using the built-in WinSAT utility.

Get-Scripts

List all external scripts and their parameter names.

Get-Services [-Name n] [-Running || -Stopped]

Get a list of services ordered by status and name. Aliased to gs

Get-SpecialFolder [-Folder f] [-All]

Return the translation of a SpecialFolder by name or show all SpecialFolders with optional search highlighting.

Get-VMConfig -Path p [-Json]

Returns a VM configuration object of the specified .vmcx VM configuration file even if the VM is not attached to a Hyper-V server.

Install-BuildTools [-Force] [-Full] [-VsWhere]

Install minimal Microsoft build and test tools required for CI/CD.

Install-Chocolatey [-Upgrade]

Can be used on new machines to install Chocolately. If already installed then checks if it is outdated and prompts to update.

Install-Docker

Installs Docker for Windows, enabling Hyper-V as a prerequisite if not already installed.

Invoke-NormalUser -Command c

Execute a given command as a non-evelated context. Aliased to nu. Convenient when you need to run as a normal user from an elevated prompt.

Invoke-SuperUser

Open a new command prompt in elevated mode. Aliased to su. Special command for ConEmu emulator.

Invoke-VsDevCmd

Invoke the Visual Studio environment batch script. Aliased to vs

New-CommandWrapper

Sepcial internal function from PowerShell Cookbook.

New-DriveMapping -DriveLetter d -Path p [-SourceDriveLabel s] [-DriveLabel l] [-Reboot] [-Force]

Create a persistent mapping of a folder to a new drive letter. (persistent SUBST)

New-Host -IP a -Name n

Adds or updates an entry in the Windows hosts file

New-RunAsShortcut -LinkPath l -TargetPath t [-Arguments a]

Creates a new elevated shortcut (.lnk file) to a given target

New-VMClone -Name n -Path p -Template t [-Checkpoint]

Create a new VM from a registered VM or an exported VM.

PrettyPrint-File -Path p [-Dedent] [-Overwrite]

Format or pretty-pretty JSON and XML files.

Remove-DockerTrash [-Volumes]

Prune unused docker containers and dangling images.

Remove-DriveMapping -DriveLetter d [-SourceDriveLabel s] [-Reboot] [-Force]

Remove a persistent mapping of a folder created by New-DriveMapping.

Remove-Locked -Name n

Remove a System-owned file or directory. Attempts multiple approaches to remove stubborn items.

Repair-Path [-Yes]

Clean up the PATH environment variable, removing duplicates, empty values, invalid paths, repairs variable substitutions, and moves paths between User and System paths appropriately. Verbose mode dumps the User and System Paths as stored in the Windows Registry.

Restart-Bluetooth [-Show]

Restarts the Bluetooth radio device on the current machine. This is useful when the radio stops communicating with a device such as a mouse. The alternative would be to reboot the system.

Set-Colors -Theme t | [-Name n -Color c [-Bgr] [-Background] [-Foreground]] [-Cmd] [-ConEmu] [-PS]

Set the color theme for command line consoles or set a specific named color.

Set-ItemOwner -Path p [-Group g]

Set the ownership of an item - folder or file - to the specified user group.

Set-OutDefaultOverride

(Internal) Helper function for Get-ChildItemColorized.

Set-PinTaskbar -Target t [-Unpin]

Pins or unpins a target item to the Windows Taskbar (currently broken!)

Set-RegistryOwner -Hive h -Key k [-Recurse]

Set full-access ownership of a specified Registry key.

Show-ColorizedContent -Filename f [-ExcludeLineNumbers]

Type the contents of a PowerShell script with syntax highlighting.

alt text

Show-Docker [-Ps] [-Containers] [-Images] [-Volumes]

Show containers and images in a single command.

alt text

Start-VMUntilReady -Name n [-Restart] [-Restore]

Start the named VM, optionally restoring the latest snapshot, and waiting until the OS provides a stable heartbeat.

Test-Elevated [-Action a ] [-Warn]

Determine if the current session is elevated and displays a warning message if not. Can be run without the warning message and simply return a boolean result.

Test-RebootPending [-Report]

Check the pending reboot status of the local computer.

Update-Gits [-Branch b] [-Project p] [-Reset]

Scan all sub-folders looking for .git directories and fetch/pull each to get latest code.

Update-Profile

Quick command line to pull latest source of this WindowsPowerShell repo from Github and update the content in $home\Documents\WindowsPowerShell.

Initialize-Machine.ps1

This is a top-level script meant to be downloaded independently from this repo and run to configure and initialize new machines. This script will download this repo to the current user's Document folder, setting it up as the default PowerShell profile. Best to download it to and run from the root of C.

Run Set-ExecutionPolicy RemoteSigned prior to running if this is the first use of PowerShell.

The Initialize-Machine script will download this repo into the MyDocuments folder, but if you have OneDrive enabled then the MyDocuments folder may differ from $home\Documents. So before initializing, you can create a junction point to MyDocuments using this command:

  1. cd $home\Documents
  2. cmd 'mklink /j WindowsPowerShell ([Environment]::GetFolderPath('MyDocuments'))

This script can optionally create a new local administrator so it runs in two phases:

  1. Log on as an administrator
  2. Download Initialize-Machine.ps1 to C:\Initialize-Machine.ps1
  3. Open an administrative PowerShell window
  4. PS> Set-ExecutionPolicy RemoteSigned
  5. PS> cd C:\
  6. PS> .\Initialize-Machine.ps1 -User <new-admin-username>
  7. ... Create new local administrator (y/n) [y]: y
  8. ... Password: *********
  9. ... Logout to log back in as <new-admin-username> (y/n) [y]: y
  10. Log on as <new-admin-username>
  11. Open an administrative PowerShell window
  12. PS> cd C:\
  13. PS> .\Initialize-Machine.ps1 -Verbose

Finally, since this updates the PowerShell console colors, you can close and reopen the console to appreciate these fantastic new colors.

Install-Programs.ps1

Automates the installation of applications, development tools, and other utilities. All applications, including Visual Studio, install unattended in about 25 minutes. It's also reentrant, skipping items already installed and installing items missing.

.\Install-Programs.ps1 [command] [-ListCommands] [-Extras] [-Enterprise] [-Verbose]

  • command - optional argument to run a single command, default is to run all commands
  • -AccessKey - optional, store your AWS access key in your private awscli config
  • -SecretKey - optional, store your AWS secret key in your private awscli config
  • -ListCommands - this argument displays all available commands
  • -Extras - this argument causes extra applications to be installed
  • -Enterprise - install VS Enterprise; default is Professional
  • -Vebose - print extra information for each command

Hyper-V and the AWSCli are installed first. Hyper-V requires a reboot which is done automatically and the script picks up where it left off immediately after you log in again.

Default applications:

  • Hyper-V (auto-reboots after install and the script continues upon logon)
  • 7Zip
  • Angular (specific version)
  • AWSCli
  • BareTail Free (installed to C:\tools)
  • Curl
  • Docker Desktop
  • Git
  • Google Chrome
  • Greenfish Icon Editor Pro
  • Greenshot
  • LINQPad
  • Macrium Reflect Free (installer)
  • mRemoteNG
  • Node.js (specific version)
  • Notepad++
  • Nuget command line
  • Robot3T
  • S3Browser
  • SharpKeys
  • SysInternals procexp and procmon
  • Visual Studio 2019 and extensions (professional or enterpise)
  • VSCode and extensions
  • Windows Terminal

Other applications included when -Extras is specified

  • Audacity audio editor
  • DateInTray (installed to C:\tools)
  • Dopamine music player
  • Paint.net
  • TreeSize Free
  • VLC
  • WiLMa (installed to C:\tools)

During the installation, hints and tips are shown highlighted in yellow and instructions are highlighted in cyan. Some are import, such as how to continue the manual installation of Macrium.

Reminders shown after a full install

Macrium Reflect

  1. Double-click the Macrium Installer icon on the desktop after VS is installed
  2. Choose Free on first screen, then Home version, and no registration is necessary

SourceTree configuration

  1. Log into choose "BitBucket" option and logon Atlassian online
  2. Enabled Advanced/"Configure automatic line endings"
  3. Do not create an SSH key

Visual Studio

  1. When installation is complete, rerun this script using the InstallVSExtensions command

.\Install-Programs.ps1 InstallVSExtensions

Consider these manually installed apps:

Profiles

Microsoft.PowerShell_profile.ps1

This the primary PowerShell profile definition script, run whenenver a new PowerShell command prompt is open or session is created. It defines aliases for some of the commands above. It also invokes special utilities unique to my environment such as integration with ConEmu, WiLMa, Chocolatey, and setting the intial working directory.

Console Prompt Customization

The PowerShell command prompt is customized as follows:

  • It is rendered in blue
  • It indicates the current working directory prefaced with "PS" for example PS D:\code>
  • If the console is elevated then the "PS" prefix is shown in red to highlight the fact that what do you, you do at your own risk!

alt text

Microsoft.PowerShellISE_profile.ps1

This is the profile definition script fort the PowerShell ISE editor. It registers ISE add-ons found in the Modules\Addons folder.

Microsoft.VSCode_profile.ps1

This is the profile definition script for the VSCode integrated PowerShell terminal. It simply invokes the primary profile script above.

Dark Selenitic Scheme

The Dark Selenitic.StorableColorTheme.ps1xml file defines the Dark Selenitic color scheme for ISE. The theme is also defined by the Themes\PSTheme_Selenitic.ps1 script.


Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
powershell (1,386
tweaks (26
powershell-script (21