Creating a Powershell Tool using the GUI RRS feed

  • Question

  • I've worked on this power shell script to remove the files from the target folder, I've completed the script and everything was going fine until one of my colleagues came up to me and asked to create the same script enhancing with the GUI :


    Param([string] $Destination_Folder, [int] $Number_of_Days, [DateTime] $Today)

    # Declaring the user interaction variables as global variable

    $global:textbox = $textbox
    $global:textbox1 = $textbox1
    $global:textbox2 = $textbox2
    $global:Last_Write_time =$Last_Write_time

    $Today = Get-Date 
    #Takes Number of Days
    #Destination folder to get the files
    $Destination_Folder ="$textBox"
    #Extention of the file which we want to delete
    $Extension ="$textBox2"
    #Checking the days using Last_Write_Time parameter based on number of days
    $Last_Write_time = $Today.AddDays(-$textbox1.Text) 
    #Scanning the files using the last_write_time and the destination_folder

    # GUI using the System.windows.Forms

    Add-Type -AssemblyName System.Windows.Forms
    Add-Type -AssemblyName System.Drawing

    $form = New-Object System.Windows.Forms.Form
    $form.Text = 'Deleting the files according to the number of days given'
    $form.Size = New-Object System.Drawing.Size(475,325)
    $form.StartPosition = 'CenterScreen'

    $OKButton = New-Object System.Windows.Forms.Button
    $OKButton.Location = New-Object System.Drawing.Point(75,220)
    $OKButton.Size = New-Object System.Drawing.Size(75,23)
    $OKButton.Text = 'OK'
    $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
    $form.AcceptButton = $OKButton

    $CancelButton = New-Object System.Windows.Forms.Button
    $CancelButton.Location = New-Object System.Drawing.Point(175,220)
    $CancelButton.Size = New-Object System.Drawing.Size(75,23)
    $CancelButton.Text = 'Cancel'
    $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
    $form.CancelButton = $CancelButton

    $label = New-Object System.Windows.Forms.Label
    $label.Location = New-Object System.Drawing.Point(20,20)
    $label.Size = New-Object System.Drawing.Size(280,20)
    $label.Text = 'Please enter the Target path below:'

    $textBox = New-Object System.Windows.Forms.TextBox
    $textBox.Location = New-Object System.Drawing.Point(10,40)
    $textBox.Size = New-Object System.Drawing.Size(260,20)

    $label1 = New-Object System.Windows.Forms.Label
    $label1.Location = New-Object System.Drawing.Point(10,80)
    $label1.Size = New-Object System.Drawing.Size(280,20)
    $label1.Text = 'Please enter number of days:'

    $textBox1 = New-Object System.Windows.Forms.TextBox
    $textBox1.Location = New-Object System.Drawing.Point(10,100)
    $textBox1.Size = New-Object System.Drawing.Size(260,20)

    $label2 = New-Object System.Windows.Forms.Label
    $label2.Location = New-Object System.Drawing.Point(10,140)
    $label2.Size = New-Object System.Drawing.Size(280,40)
    $label2.Text = 'Please enter the type of extension that you want to delete:'

    $textBox2 = New-Object System.Windows.Forms.TextBox
    $textBox2.Location = New-Object System.Drawing.Point(10,180)
    $textBox2.Size = New-Object System.Drawing.Size(260,20)

    $form.Topmost = $true

    $result = $form.ShowDialog()
    $OKButton_OnClick = 

    function CalculatingtheDays

        $Files = Get-Childitem $textbox.Text -Include $textbox2.Text -Recurse | Where {$_.LastWriteTime -le "$Last_Write_time"} -ErrorAction Stop 
        if ($textbox1.Text -gt $Last_Write_time)
            write-host "There are no files Present <g class="gr_ gr_67 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="67" id="67">refering</g> to the days you have entered" -ForegroundColor "Green" #It will check for the files which are created greater than the number of days
                Write-host "Files are being Deleted!!" -ForegroundColor Yellow

    foreach ($File in $Files)     {
              if ($File -ne $NULL)
                  write-host "Removing Files $File" -ForegroundColor "Red"
                  Remove-Item $File.FullName | out-null
                  Write-host "cleanup has complete. There are no more files to delete!" -Foregroundcolor White


    When I enter the number of days, destination folder and the extension, nothing is happening. Wondering where exactly I am doing wrong.

    <g class="gr_ gr_80 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="80" id="80">Apreciate</g> your response.



    • Edited by Ragotham Tuesday, August 28, 2018 5:46 AM
    Tuesday, August 28, 2018 5:45 AM

All replies

  • Please post your code correctly.  Use the tool provided on the edit bar.

    You cannot assign a function to a variable.  It serves no purpose.

    Where did you find this code?  We cannot debug and fix large amounts of code.  You need to ask a specific question.


    Tuesday, August 28, 2018 6:10 AM
  • What is it you need to do?  What is not happening.   You have code that can never be executed.  Why?

    Here is a good place to begin learning how to use forms: https://info.sapien.com/index.php/guis

    I asked you to fix your original post.  Please do that first.


    • Edited by jrv Tuesday, August 28, 2018 7:02 AM
    Tuesday, August 28, 2018 7:01 AM
  • Hello,

    In addition, you haven't defined at your function any file by extension... so in case your function work, it will delete all files allocated at specified folder.


    Tuesday, August 28, 2018 11:09 AM