Saving PowerShell User Defaults

We are about to enter “Color Week” with plenty of tips how you can choose better colors for the PowerShell ISE editor and the console. Most changes that you apply to PowerShell are not saved. PowerShell ISE does save some of the color settings, but a more robust way is to place your customizations in one of PowerShell’s profile scripts.

Anything that should be run for any PowerShell host (console as well as PowerShell ISE or any other PowerShell-enabled program) goes here:

PS C:> $profile.CurrentUserAllHosts

Anything that applies to a specific host like the console only, or the PowerShell ISE only, goes here:

PS C:>  $profile.CurrentUserCurrentHost

Note the “XXX” in the path. You need to run the above line inside the host you want to target. The line returns different paths, depending on the host you are running.

Note also that these calls just provide the path to the profile script. It does not exist by default. You may have to create it, along with the folder “WindowsPowerShell”. When the profile script does exist, it is executed whenever a PowerShell host is launched.

Just make sure script execution is turned on. So you may have to one-time enable script execution, for example like this:

PS> Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned -Force 

Twitter This Tip! ReTweet this Tip!

Finding ASCII Codes

Here is an easy way to find the ASCII code for any character you may have scraped from a website, or found in a script that you copied from the internet.

Simply open a PowerShell, and enter this:

# paste character(s)  inside the quotes 
$text = ''
foreach($char in [char[]]$text)
  'Character {0,-3} Decimal {1,-5} Hex {1,-4:X}' -f $char, [int]$char

Next, paste the character(s) inside the quotes, and run the code. To test-drive, run the following command in PowerShell:

PS C:> charmap

This opens the character map where you can pick a font, like DingBats for example, and one or more characters. Copy them to the clipboard, then paste them into the PowerShell code above. When you run the code, it returns the ASCII values for the selected characters in decimal and hex form. They should match the values found in the status bar of the character mapper utility.

Twitter This Tip! ReTweet this Tip!

Checking Hard Drive Size (Local and Remote)

WMI can provide the raw data about hard drive size and free space. PowerShell then takes that information and provides user-friendly results like this:

PS C:> # local
PS C:> Get-HardDriveSize

DriveLetter Free(GB) Size(GB) Percent
----------- -------- -------- -------
C:             823,7    942,3    87,4

PS C:> # remote
PS C:> Get-HardDriveSize -ComputerName server2 -Credential server2Tobias

DriveLetter Free(GB) Size(GB) Percent
----------- -------- -------- -------
C:              87,3    436,9      20
D:               5,3       25    21,3

Here is the code:

function Get-HardDriveSize


  # get calculated properties:
  $prop1 = @{
    Name = 'DriveLetter'
    Expression = { $_.DeviceID }

  $prop2 = @{
    Name = 'Free(GB)'
    Expression = { [Math]::Round(($_.FreeSpace / 1GB),1) }

  $prop3 = @{
    Name = 'Size(GB)'
    Expression = { [Math]::Round(($_.Size / 1GB),1) }

  $prop4 = @{
    Name = 'Percent'
    Expression = { [Math]::Round(($_.Freespace * 100 / $_.Size),1) }

  # get all hard drives
  Get-WmiObject -ClassName Win32_LogicalDisk @PSBoundParameters -Filter "DriveType=3" | 
  Select-Object -Property $prop1, $prop2, $prop3, $prop4


Twitter This Tip! ReTweet this Tip!

Enabling Remote Administration

PowerShell 2+

Many older DCOM-based commands require a “Remote Administration Firewall Exception” to access remote systems. This includes cmdlets like Get-WmiObject.

One easy way of enabling this is to run the following command in a PowerShell with Administrator privileges:

netsh firewall set service remoteadmin enable

Although this command is considered deprecated, it is still working and one of the easiest ways of configuring the firewall.

Twitter This Tip! ReTweet this Tip!

Finding Auto Starts

PowerShell 3+

If you’d like to know which programs start automatically on your machine, WMI may help:

PS C:> Get-CimInstance -ClassName Win32_StartupCommand | Select-Object -Property Name, Location, User, Command, Description

Name        : OneDrive
Location    : HKUS-1-5-21-2012478179-265285931-690539891-1001SOFTWAREMicrosoftWindowsCurrentVersionRun
User        : DESKTOP-7AAMJLFtobwe
Command     : "C:UserstobweAppDataLocalMicrosoftOneDriveOneDrive.exe" /background
Description : OneDrive

Name        : Bluetooth
Location    : Common Startup
User        : Public
Command     : C:PROGRA~1WIDCOMMBLUETO~1BTTray.exe 
Description : Bluetooth

Name        : Snagit 12
Location    : Common Startup
User        : Public
Command     : C:PROGRA~2TECHSM~1SNAGIT~1Snagit32.exe 
Description : Snagit 12

Name        : RTHDVCPL
Location    : HKLMSOFTWAREMicrosoftWindowsCurrentVersionRun
User        : Public
Command     : "C:Program FilesRealtekAudioHDARtkNGUI64.exe" -s
Description : RTHDVCPL


Twitter This Tip! ReTweet this Tip!

Replacing CSV File Headers

PowerShell 2+

When you read in CSV data and would like to rename the CSV headers, here is a simple approach: just read in the text line by line, and skip the first line (which holds the CSV headers). Then, replace the headers with a list of your own header names:

$header = NewHeader1’, 'NewHeader2', 'NewHeader3'

Get-Content N:somepathtofileuserlist.csv -Encoding Default | 
 Select-Object -Skip 1 |
 ConvertFrom-CSV -UseCulture -Header $header

Twitter This Tip! ReTweet this Tip!