In many scenarios, we will get the requirement to upload the reports, tracker from one location to another location , so i decided to write this post to explain how to upload files like excel, txt… with the help of Powershell GUI. Instead of thinking high level, here i had split the three simple task to achieve the file upload functionality in the Powershell.
- Create Simple PowerShell UI for browsing the location of files with one textbox and two button type (upload & cancel) with help of system.Windows.Forms
- In Browse button click event, you can perform the browse with the specific file that need to be upload to destination folder using New-Object -TypeName System.Windows.Forms.OpenFileDialog. These make the selection of files or folders easy for users and prevent mistakes being made in file paths.
- In upload button click event, you can perform the Move action on the selected file to place on Target location.
When designing PowerShell scripts, I have often found it useful to incorporate a file or folder dialogue to select files or folders. These make the selection of files or folders easy for users and prevent mistakes being made in file paths.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
####################################################### #Project : Upload the files using Powershell #Developer : Thiyagu S (dotnet-helpers.com) #Tools : PowerShell 5.1.15063.1155 #E-Mail : mail2thiyaguji@gmail.com ####################################################### #Form class $Forms = 'system.Windows.Forms.Form' #Button class $Button = 'system.Windows.Forms.Button' #Getting the font details for applying the fields $FontStyle = 'Microsoft Sans Serif,10,style=Bold' $SysDrawPoint = 'System.Drawing.Point' # setting InitialDirectory which open as intially during the click of browse button $InitialDirectory = "C:\New folder" Add-Type -AssemblyName System.Windows.Forms [System.Windows.Forms.Application]::EnableVisualStyles() #The New-Object cmdlet creates an instance of a .NET Framework or COM object. #Specifies the fully qualified name of the .NET Framework class. You cannot specify both the TypeName parameter and the ComObject parameter. $TrackerFileUpload = New-Object -TypeName $Forms $TrackerFileUpload.ClientSize = '400,114' $TrackerFileUpload.text = 'dotnet-helpers.com - Please upload Daily Tracker' $TrackerFileUpload.TopMost = $false $txtFileName = New-Object -TypeName system.Windows.Forms.TextBox $txtFileName.width = 280 $txtFileName.height = 20 $txtFileName.location = New-Object -TypeName $SysDrawPoint -ArgumentList (107,39) $txtFileName.Font = $FontStyle $btnFileBrowser = New-Object -TypeName $Button $btnFileBrowser.BackColor = '#1a80b6' $btnFileBrowser.text = 'Browse' $btnFileBrowser.width = 96 $btnFileBrowser.height = 30 $btnFileBrowser.location = New-Object -TypeName $SysDrawPoint -ArgumentList (200,78) $btnFileBrowser.Font = $FontStyle $btnFileBrowser.ForeColor = '#ffffff' $btnUpload = New-Object -TypeName $Button $btnUpload.BackColor = '#00FF7F' $btnUpload.text = 'Upload' $btnUpload.width = 96 $btnUpload.height = 30 $btnUpload.location = New-Object -TypeName $SysDrawPoint -ArgumentList (291,78) $btnUpload.Font = $FontStyle $btnUpload.ForeColor = '#000000' $lblFileName = New-Object -TypeName system.Windows.Forms.Label $lblFileName.text = 'Choose File' $lblFileName.AutoSize = $true $lblFileName.width = 25 $lblFileName.height = 10 $lblFileName.location = New-Object -TypeName $SysDrawPoint -ArgumentList (20,40) $lblFileName.Font = $FontStyle #Adding the textbox,buttons to the forms for displaying $TrackerFileUpload.controls.AddRange(@($txtFileName, $btnFileBrowser, $lblFileName, $btnUpload)) #Browse button click event $btnFileBrowser.Add_Click({ Add-Type -AssemblyName System.windows.forms | Out-Null #Creating an object for OpenFileDialog to a Form $OpenDialog = New-Object -TypeName System.Windows.Forms.OpenFileDialog #Initiat browse path can be set by using initialDirectory $OpenDialog.initialDirectory = $initialDirectory #Set filter only to upload Excel file $OpenDialog.filter = '.xlsx (*(.xlsx)) | *.xlsx | *.txt' $OpenDialog.ShowDialog() | Out-Null $filePath = $OpenDialog.filename #Assigining the file choosen path to the text box $txtFileName.Text = $filePath $TrackerFileUpload.Refresh() }) #Upload button click eventy $btnUpload.Add_Click({ #Set the destination path $destPath = 'C:\EMS\Destination' #$txtFileName.Text = "" Copy-Item -Path $txtFileName.Text -Destination $destPath }) $null = $TrackerFileUpload.ShowDialog() $txtFileName.Text = "" |
Output:
What do you think?
I hope you have an idea of How To upload the files with help of PowerShell GUI. I would like to have feedback from my posts readers. Your valuable feedback, question, or comments about this article are always welcome.