Find invalid characters using PowerShell

Have you ever copied code from a web site or an email and have it not work because some of the characters auto-corrected to weird quotes or dashes or the like? I have, and it can be a real hassle finding those characters because they look so much like the normal characters.

I wrote this module to find those characters.

From the module description:

Finds out of range encoding (ASCII, ANSI, Unicode) of characters in a file or object. The default “in range” is 32 to 126, most of the “printable” ASCII.
Each line is printed to the console, preceded by a line number. In range characters are displayed in black and white. Out of range characters are displayed in yellow with a red background. At the end of the output is a log of each out of range character; listing line number, character number, the character as it is displayed, and the encoding value.
A custom range can be set by passing an array of in-range INTs. The range does not need to be consecutive, it just needs to be an array of integers.
This module will be especially useful in checking code pasted from email or the web with might have unacceptable characters.
Note that out of range characters are not always wrong, but in the wrong spot can cause problems.

<#
.Synopsis
   Finds out of range encoding of characters in a file or object 
.DESCRIPTION
   Finds out of range encoding (ASCII, ANSI, Unicode) of characters in a file or object. The default "in range" is 32 to 126, most of the "printable" ASCII.
   Each line is printed to the console, preceded by a line number. In range characters are displayed in black and white. Out of range characters are displayed in yellow with a red background. At the end of the output is a log of each out of range character; listing line number, character number, the character as it is displayed, and the encoding value.
   A custom range can be set by passing an array of in-range INTs. The range does not need to be consecutive, it just needs to be an array of integers.
   This module will be especially useful in checking code pasted from email or the web with might have unacceptable characters. 
   Note that out of range characters are not always wrong, but in the wrong spot can cause problems.
.EXAMPLE
   Find-OutOfRangeCharacters -Filename .\fate.ps1
.EXAMPLE
   Find-OutOfRangeCharacters -Object $(Get-Content -Path .\fate.ps1) -Range $(32..96)
.EXAMPLE
   Find-OutOfRangeCharacters -Object "hello wörld"
#>
function Find-OutOfRangeCharacters
{
    [CmdletBinding()]
    Param
    (
        # File of data to be searched
        [Parameter(ParameterSetName='Filename')]
        [ValidateScript({Test-Path $_})]
        [string]
        $Filename,

        # Data to be searched
        [Parameter(ParameterSetName='Object')]
        [string[]]
        $Object,

        # INT array of in-range encoding values
        [int[]]
        $Range = 32 .. 126
    )

#region retrieve data
    if ($Object) 
    {
        $c = $Object
    }
    else
    {
        $c = Get-Content $Filename #-ErrorAction Stop
    }
#endregion

    $message = ""

    # loop through each line
    for ($i = 0; $i -lt $c.Length; $i++)
    { 
        # need to display "$i + 1" because arrays count from 0 but humans count from 1
        $PrettyLineNumber = "[" + $($i + 1).ToString("000") + "]" 
        Write-Host -Object $PrettyLineNumber  -NoNewline -ForegroundColor White -BackgroundColor Blue
        $line = $c[$i]

        # loop through each character in the line
        for ($k = 0; $k -lt $line.Length; $k++)
        {
            # get encoding value for the character
            $CharValue = [int][char]$line[$k]

            if ($CharValue -notin $Range) 
            {
                # need to display "$i + 1" and "$k + 1" because arrays count from 0 but humans count from 1
                Write-Host -Object $line[$k] -NoNewline -ForegroundColor Yellow -BackgroundColor Red
                $message += "Line # $($i + 1), character # $($k + 1), displays as `"$($line[$k])`", encoding value $CharValue `n" 
            }
            else
            {
                Write-Host -Object $line[$k] -NoNewline -ForegroundColor White -BackgroundColor Black 
            }
        }
        # write end of line
        Write-Host

    }
    # write log of out of range characters found
    Write-Output $message
}

Get a random decimal number between 0 and 1 in PowerShell

Get-Random -Maximum ([Double]1)

How does this work? The Get-Random cmdlet returns a Double (that’s a decimal number) when the Maximum parameter is a Double. If we just wrote -Maximum 1, then PowerShell would interpret the 1 as an integer. So we cast the 1 as a Double by writing it as [Double]1. Since [Double]1 is not a simple value or variable, PowerShell will misinterpret this as a parameter. So we wrap if in parentheses to get: ([Double]1). This grouping tells PowerShell to evaluate it first, then use that result as the parameter.

Minimum defaults to 0, so we don’t need to specify it.

How this helps!

vi cheatsheet

Get the PDF here.

The bare minimum vi.

How to open a new or existing file in vi

vi filename

File navigation

Use [Page Up] , [Page Down]  to page through the file. Use the arrow keys to move the cursor.

Command mode and insert mode

When you first start vi, you are in command mode

To switch to insert mode

i

Once in insert mode, you can type in your text file.

To switch from insert mode to command mode

[ESC]

Command mode

Quit without saving changes

:q!

Quit

:q

Write (save)

:w

Write a copy of the file to another name

:w filename

Search for a string (replace “search” with the string you are searching for)

:/search

Global search and replace (replace “search” and “replace” with the strings you are searching and replacing)

:%s/search/replace/

Find Duplicate Files with PowerShell

This one-liner will find duplicate files in the current directory and all sub-directories. It uses hash values of the files, so it doesn’t matter if the file names have changed. If the content is the same, the hash will be the same and it will be considered a duplicate.

# find duplicate files
# Kenward Bradley 2016-12-29
Get-ChildItem -Recurse | Get-FileHash | Group-Object -Property Hash | Where-Object Count -GT 1 | foreach {$_.Group | select Path, Hash}

Fixed my iTunes skipping problem

Since I upgraded my PC to Vista, iTunes had been skipping. Under XP on the same machine, iTunes had been playing OK. Playback would stutter/skip several times a minute. It was really annoying. I couldn’t find a fix anywhere, although all the fixes seemed to point to Quicktime or upgrading codecs. However my fix turned out to be something else. In Quicktime, I changed “Play audio using” from “Direct Sound” to “Windows Audio Session”. No more skipping! Apparently the “Windows Audio Session” option became available with iTunes 9.

DrinkGen – a tool for generating mixed drink recipies

Click here to generate mixed drink recipies with DrinkGen.

Some examples of drinks invented with DrinkGen:

Hopeful Cocktail
3 oz. Whisky
Juice of 1 a Lemon
Fill cocktail glass with Cola. Add cracked ice.

Mathematical
1½ oz. Whisky
1 oz. Tonic water
Shake with cracked ice, and strain into in a collins glass.

Horrendous Albany
1 oz. Vodka
3 oz. Whisky
Stir, in a rocks glass. Add cracked ice.

Geek List of Movies

How many of these have you seen?

Seeing a “Geek Movies” list on Facebook inspired me to make this list of movies. I’d say its my geek list of movies. Not the best movies ever made. Not the best science fiction either. Not even a list of all the movies I’ve seen or plan on seeing. Some I don’t even like. Just that I think they in some way have Geek Cachet. Let’s just call it a Geek List of Movies.

(x) 12 Monkeys
(x) 1984
(x) 2001
( ) 2010
(x) 28 Days Later
(x) 28 Weeks Later
(x) A Scanner Darkly
(x) A.I. Artificial Intelligence
(x) Adventures of Buckaroo Banzai
(x) Aeon Flux
(x) Akira
(x) Alien
( ) Alien 3
(x) Alien Resurrection
(x) Aliens
(x) Altered States
Continue reading “Geek List of Movies”