Monday 28 April 2014

Debug: Write out character code for all characters given string

Summary:
Found this trying to figure out if there were any new lines in text file

Reference:
Unknown

Code (Original):
$enc = New-Object System.Text.ASCIIEncoding
$string.ToCharArray() | Foreach-Object { Write-Host "$($_) $($enc.GetBytes($_))" }

Code (Modified):
$enc = New-Object System.Text.ASCIIEncoding
(Get-Content .\Email.txt).ToCharArray() | Foreach-Object { Write-Host "$($_) $($enc.GetBytes($_))" }

Function: Create registry key

Summary:
Creates registry key along with sub keys, first attempt at powershell not all work is mine but I have lost site where I borrowed some of the code from.

Reference:
Unknown

Code (Original):
Function New-RegistryKey([string]$key,[string]$Name,[string]$type,[string]$value,[boolean]$force)
    {
    #Split the registry path into its single keys and save
    #them in an array, use \ as delimiter:
    #$subkeys = $key.split("\")
    $subkeys = $key.split("\")
   
    #Do this for all elements in the array:
    foreach ($subkey in $subkeys)
        {
        #Extend $currentkey with the current element of
        #th
        e array:
        $currentkey += ($subkey + '\')
        #Check if $currentkey already exists in the registry
        if (!(Test-Path $currentkey))
            {
            #If no, create it and send Powershell output
            #to null (don't show it)
            New-Item -Type String $currentkey | Out-Null
            }
        }
    #Set (or change if alreday exists) the value for $currentkey
    #if ($force -eq $True)
    if ($force -eq $True)
            {
            New-ItemProperty $CurrentKey $Name -Value $Value -Type $type -Force
            }
        else
            {
            if ((Get-ItemProperty $CurrentKey).$Name -eq $null)
                    {
                    New-ItemProperty $CurrentKey $Name -Value $Value -Type $type               
                    }
            }
    }

Usage:
Types:
  • Binary - Binary Data
  • Dword - A number that is a vlie UInt32
  • ExpandString - A strong that can contain environment variables that are dynamically expanded
  • MultiString - A multiline string
  • String - Any string value
  • QWord - 8 bytes of binary data
the $True or $False refers to overwrite of the key

New-RegistryKey "Hive:Path" "KeyName" "ValueName" "Type" "Value" "$Trueor$False"

New-RegistryKey "HKLM:\Software\Boo" "Hoo" "String" ";'o(" "$True"
will create registry key and overwrite:
[HKEY_LOCAL_MACHINE\SOFTWARE\Boo]
"Hoo"=";'o("