This code is a PowerShell script that creates a simple...

July 2, 2025 at 04:09 PM

Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Drawing # Create the main form $form = New-Object System.Windows.Forms.Form $form.Text = 'File Hash Calculator' $form.Size = New-Object System.Drawing.Size(600,400) $form.StartPosition = 'CenterScreen' # Input field for UNC path $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 = 'Enter UNC Path:' $form.Controls.Add($label) $textBoxPath = New-Object System.Windows.Forms.TextBox $textBoxPath.Location = New-Object System.Drawing.Point(10,40) $textBoxPath.Size = New-Object System.Drawing.Size(560,20) $form.Controls.Add($textBoxPath) # Button to compute hash $buttonCompute = New-Object System.Windows.Forms.Button $buttonCompute.Location = New-Object System.Drawing.Point(10,70) $buttonCompute.Size = New-Object System.Drawing.Size(100,23) $buttonCompute.Text = 'Calc Hash' $buttonCompute.Add_Click({ $path = $textBoxPath.Text if (Test-Path $path) { $hashes = Get-ChildItem -Path $path -Recurse -File | ForEach-Object { Get-FileHash -Path $_.FullName | Select-Object -ExpandProperty Hash} $outputBox.Text = $hashes | Out-String } else { $outputBox.Text = "Not Found." } }) $form.Controls.Add($buttonCompute) # Output text box $outputBox = New-Object System.Windows.Forms.TextBox $outputBox.Location = New-Object System.Drawing.Point(10,100) $outputBox.Size = New-Object System.Drawing.Size(560,250) $outputBox.Multiline = $true $outputBox.ScrollBars = 'Vertical' $form.Controls.Add($outputBox) # Copy to clipboard button $buttonCopy = New-Object System.Windows.Forms.Button $buttonCopy.Location = New-Object System.Drawing.Point(120,70) $buttonCopy.Size = New-Object System.Drawing.Size(100,23) $buttonCopy.Text = 'Copy' $buttonCopy.Add_Click({ $copyText = $outputBox.Text [System.Windows.Forms.Clipboard]::SetText($copyText) }) $form.Controls.Add($buttonCopy) # Show the form $form.ShowDialog()

This code is a PowerShell script that creates a simple graphical user interface (GUI) application for calculating file hashes in a specified directory path using the .NET Windows Forms library. Here's what each part of the code does:

  1. Add-Type:

    • Add-Type -AssemblyName System.Windows.Forms and Add-Type -AssemblyName System.Drawing load the necessary .NET assemblies to create Windows Forms GUIs and use graphical elements.
  2. Form Setup:

    • A main form ($form) is created with the title "File Hash Calculator" and configured with a size of 600x400 pixels. It opens in the center of the screen.
  3. Input Field:

    • A label ($label) and a text box ($textBoxPath) are added to the form to allow the user to input a UNC (Universal Naming Convention) path. This is where the user specifies the directory to compute the file hashes.
  4. "Calc Hash" Button:

    • A button ($buttonCompute) labeled "Calc Hash" is added.
    • When clicked, it retrieves the text entered in $textBoxPath, checks if the path exists using Test-Path, and then calculates the hash values (e.g., SHA256) for all files in the specified directory (recursively). It uses Get-FileHash to compute the hash.
    • If the path is valid, it displays the computed hash values in the $outputBox text box. If the path doesn't exist, it shows "Not Found."
  5. Output Text Box:

    • A multi-line text box ($outputBox) with vertical scrolling is created to display the computed hashes or error messages.
  6. "Copy" Button:

    • Another button ($buttonCopy) labeled "Copy" is added.
    • When clicked, it copies the content of the $outputBox to the clipboard using [System.Windows.Forms.Clipboard]::SetText.
  7. Displaying the Form:

    • The form is finally displayed using $form.ShowDialog(), which makes it interactive for the user.

Summary:

This script creates a GUI-based file hash calculator. The user enters a directory path in the input field, clicks "Calc Hash," and sees the hash values for all files in that directory. The calculated hashes can also be copied to the clipboard by clicking the "Copy" button. It is a simple tool for verifying file integrity or identifying files by their hash values.

Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node