Search code examples
powershelltrimpowershell-5.0

trim() not working for license key tool


Personal skill level with PS: Low (Student)

Goal: Trying to find the Windows 10 license key, display it in a window as well as automatically copy it to the clipboard.

Issue: My output always results in spaces at the beginning and end of the string. I have tried trim(), trimstart(), etc. Nothing has worked thus far. I would not mind so much by the spaces are then saved with the key to the clipboard - making the functionality kinda useless, or at least tedious.

I laid out some notations in the code to identify the problem.

The code (result is below as well):

    Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
[void][reflection.assembly]::Load("System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")

#Obtain Windows 10 Key
$License = wmic path SoftwareLicensingService get OA3xOriginalProductKey

# FAULT - Used to trim key 
$Result = $License.Trim("OA3xOriginalProductKey")

#Used to isolate the spaces in the result. Attempt to see if the spaces occur after the trim, or during the trim.
#The spaces are within the "|" rather than outside of the "|" - meaning that the Trim() is not working properly.
#PLEASE HELP!
$Result = "|" + $Result.Trim() + "|"

#Used to copy key to clipboard
$Result | clip.exe


$form = New-Object System.Windows.Forms.Form 
$form.Text = "KeySniffer 1.0"
$form.Size = New-Object System.Drawing.Size(280,140) 
$form.StartPosition = "CenterScreen"

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(30,70)
$OKButton.Size = New-Object System.Drawing.Size(60,23)
$OKButton.Text = "OK"
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20) 
$label.Size = New-Object System.Drawing.Size(280,20) 
$label.Text = "Key has been copied to clipboard"
$form.Controls.Add($label) 

$textBox = New-Object System.Windows.Forms.TextBox
#Put a trim here as a desperate swing in the dark
#The "-" indicate that the spaces occur within the "$Result"
$textBox.Text = "-" + $Result.Trim() + "-"
$textBox.Location = New-Object System.Drawing.Point(10,40) 
$textBox.Size = New-Object System.Drawing.Size(200,20) 
$form.Controls.Add($textBox) 

$form.Topmost = $True

$form.Add_Shown({$textBox.Select()})
$result = $form.ShowDialog()

if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
    $x = $textBox.Text
    $x
}



The result:

-|  ****-*****-*****-*****-*****   |-

Solution

  • Seems you already took out text you don't want with this

    $Result = $License.Trim("OA3xOriginalProductKey")
    

    Now let's try only keep content we want on top of it by design a pattern, that only keeps numbers, upper case chars, and "-". Also, we need to remove extra lines.

    $pattern = '[^0-9A-Z-]'
    $Result = ($Result -replace $pattern, '').trim() -ne ""
    

    Now you can check out the result, everything unwanted should be gone

    $Result = "|" + $Result + "|"
    $Result