Using GUI Textbox.Text for creating a Folder and adding subfolder - powershell

I am trying to create a GUI which always should create a folder with the string from a textbox and after add oder Item and folder
With the First Button i create the folder using the string from the textbox
With the Butto "save" another folder should be added to the new created folder
Here my script
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$window = New-Object System.Windows.Forms.Form
$window.Width = 1300
$window.Height = 1000
$window.Text = "Project Management Dashboard"
$Label1 = New-Object System.Windows.Forms.Label
$Label1.Location = New-Object System.Drawing.Size(10,10)
$Label1.Text = "Creator"
$Label1.AutoSize = $True
$Label1.ForeColor = "Black"
$window.Controls.Add($Label1)
$windowTextBox1 = New-Object System.Windows.Forms.TextBox
$windowTextBox1.Location = New-Object System.Drawing.Size(10,40)
$windowTextBox1.Size = New-Object System.Drawing.Size (300,500)
$window.Controls.Add($windowTextBox1)
$windowsbuttonquery = New-Object System.Windows.Forms.Button
$windowsbuttonquery.Location = New-Object System.Drawing.Point(400,800)
$windowsbuttonquery.Size = New-Object System.Drawing.Size(175,80)
$windowsbuttonquery.Text = "Project Package"
$windowsbuttonquery.ForeColor = "Black"
$window.Controls.Add($windowsbuttonquery)
$windowsbuttonquery.Add_Click({
New-Item -Path C:\Test_Ps1 -Name $windowTextBox1.Text -ItemType directory
}
)
$save = New-Object System.Windows.Forms.Button
$save.Location = New-Object System.Drawing.Point(700,800)
$save.Size = New-Object System.Drawing.Size(175,80)
$save.Text = "SAVE"
$save.ForeColor = "Black"
$window.Controls.Add($save)
$save.Add_Click({
$to = "c:\Test_Ps1\$windowTextBox1.Text"
Copy-Item -Path C:\Test_Ps1\Z -Destination $to
$window.Dispose()
}
)
[void]$window.ShowDialog()
Thanks for help.

Related

get acl from folders with file open dialog

I have built a little gui for getting the acl permissions for folders. with the path button i want to specify the folder path with a folder browser dialog and with the permissions button i want to get the acl. unfortunately the permissions button don't work because it can't get the folder path from the get-folder function. what's wrong with the function?
#################################################### Functions #######################################################
$path = Function Get-Folder ($initialDirectory)
{
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")|Out-Null
$Ordnername = New-Object System.Windows.Forms.FolderBrowserDialog
$Ordnername.Description = "Ordner auswählen"
$Ordnername.rootfolder = "MyComputer"
if($Ordnername.ShowDialog() -eq "OK")
{
$Ordner += $Ordnername.SelectedPath
}
return $Ordner
}
############################################## GetPermissions function
function GetPermissions{
#$Folder = get-folder
$Result = (Get-ACL $path).access | Format-Table IdentityReference,FileSystemRights,AccessControlType,IsInherited,InheritanceFlags | Out-string
$outputBox.Text = $Result
}
function Close{
$Form.Close()
}
###################### CREATING PS GUI TOOL #############################
#### Form settings #################################################################
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$Form = New-Object System.Windows.Forms.Form
$Form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle #modifies the window border
$Form.Text = "Folder Permissions"
$Form.Size = New-Object System.Drawing.Size(1120,330)
$Form.StartPosition = "CenterScreen" #loads the window in the center of the screen
$Form.BackgroundImageLayout = "Zoom"
$Form.MinimizeBox = $False
$Form.MaximizeBox = $False
$Form.WindowState = "Normal"
$Form.SizeGripStyle = "Hide"
$Icon = [system.drawing.icon]::ExtractAssociatedIcon($PSHOME + "\powershell.exe")
$Form.Icon = $Icon
#### Input window with "Folder Path" label ##########################################
#$InputBox = New-Object System.Windows.Forms.TextBox
#$InputBox.Location = New-Object System.Drawing.Size(10,50)
#$InputBox.Size = New-Object System.Drawing.Size(180,20)
#$Form.Controls.Add($InputBox)
#$Label2 = New-Object System.Windows.Forms.Label
#$Label2.Text = "Folder Path:"
#$Label2.AutoSize = $True
#$Label2.Location = New-Object System.Drawing.Size(15,30)
#$Form.Controls.Add($Label2)
#### Group boxes for buttons ########################################################
$groupBox = New-Object System.Windows.Forms.GroupBox
$groupBox.Location = New-Object System.Drawing.Size(10,95)
$groupBox.size = New-Object System.Drawing.Size(180,180)
$groupBox.text = "Controls:"
$Form.Controls.Add($groupBox)
###################### BUTTONS ##########################################################
#### Path ###################################################################
$Path = New-Object System.Windows.Forms.Button
$Path.Location = New-Object System.Drawing.Size(10,10)
$Path.Size = New-Object System.Drawing.Size(150,60)
$Path.Text = "Path"
$Path.Add_Click({Get-folder})
$Path.Cursor = [System.Windows.Forms.Cursors]::Hand
$Form.Controls.Add($Path)
#### Permissions ###################################################################
$Permissions = New-Object System.Windows.Forms.Button
$Permissions.Location = New-Object System.Drawing.Size(15,25)
$Permissions.Size = New-Object System.Drawing.Size(150,60)
$Permissions.Text = "Permissions"
$Permissions.Add_Click({GetPermissions})
$Permissions.Cursor = [System.Windows.Forms.Cursors]::Hand
$groupBox.Controls.Add($Permissions)
#### Close ###################################################################
$Close = New-Object System.Windows.Forms.Button
$Close.Location = New-Object System.Drawing.Size(15,100)
$Close.Size = New-Object System.Drawing.Size(150,60)
$Close.Text = "Close"
$Close.Add_Click({Close})
$Close.Cursor = [System.Windows.Forms.Cursors]::Hand
$groupBox.Controls.Add($Close)
###################### END BUTTONS ######################################################
#### Output Box Field ###############################################################
$outputBox = New-Object System.Windows.Forms.RichTextBox
$outputBox.Location = New-Object System.Drawing.Size(200,20)
$outputBox.Size = New-Object System.Drawing.Size(900,265)
$outputBox.Font = New-Object System.Drawing.Font("Consolas", 8 ,[System.Drawing.FontStyle]::Regular)
$outputBox.MultiLine = $True
$outputBox.ScrollBars = "Vertical"
$Form.Controls.Add($outputBox)
##############################################
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()
The reason for that mainly has to do with scoping, plus you should not write the function as $path = Function ... making it a call to the function.
Also, $Ordner += $Ordnername.SelectedPath is wrong, because you have never defined what $Ordner is in the function.
Below a rewrite of your code where I took the liberty to change some variable names to make them more self-explanatory:
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
###################### CREATING PS GUI TOOL #############################
# define your selected path variable and initialize to nothing
$SelectedPath = $null
#################################################### Functions #######################################################
function Get-Folder {
$Ordnername = New-Object System.Windows.Forms.FolderBrowserDialog
$Ordnername.Description = "Ordner auswählen"
$Ordnername.rootfolder = "MyComputer"
if($Ordnername.ShowDialog() -eq "OK") {
$script:SelectedPath = $Ordnername.SelectedPath # use script scoping here
$outputBox.Text = $script:SelectedPath
}
}
############################################## GetPermissions function
function Get-Permissions {
$outputBox.Text = '' # clear the textbox
if (-not [string]::IsNullOrWhiteSpace($script:SelectedPath)) {
$Result = (Get-ACL $script:SelectedPath).Access | # use script scoping here
Format-Table IdentityReference,FileSystemRights,AccessControlType,IsInherited,InheritanceFlags | Out-string
$outputBox.Text = $script:SelectedPath + "`r`n`r`n" + $Result
}
}
#### Form settings #################################################################
$Form = New-Object System.Windows.Forms.Form
$Form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle #modifies the window border
$Form.Text = "Folder Permissions"
$Form.Size = New-Object System.Drawing.Size(1120,330)
$Form.StartPosition = "CenterScreen" #loads the window in the center of the screen
$Form.BackgroundImageLayout = "Zoom"
$Form.MinimizeBox = $False
$Form.MaximizeBox = $False
$Form.WindowState = "Normal"
$Form.SizeGripStyle = "Hide"
$Icon = [System.Drawing.Icon]::ExtractAssociatedIcon((Join-Path -Path $PSHOME -ChildPath 'powershell.exe'))
$Form.Icon = $Icon
#### Group boxes for buttons ########################################################
$groupBox = New-Object System.Windows.Forms.GroupBox
$groupBox.Location = New-Object System.Drawing.Size(10,95)
$groupBox.size = New-Object System.Drawing.Size(180,180)
$groupBox.text = "Controls:"
$Form.Controls.Add($groupBox)
###################### BUTTONS ##########################################################
#### Path button ###################################################################
$PathButton = New-Object System.Windows.Forms.Button
$PathButton.Location = New-Object System.Drawing.Size(10,10)
$PathButton.Size = New-Object System.Drawing.Size(150,60)
$PathButton.Text = "Path"
$PathButton.Cursor = [System.Windows.Forms.Cursors]::Hand
$PathButton.Add_Click({Get-Folder})
$Form.Controls.Add($PathButton)
#### Permissions button ###################################################################
$Permissions = New-Object System.Windows.Forms.Button
$Permissions.Location = New-Object System.Drawing.Size(15,25)
$Permissions.Size = New-Object System.Drawing.Size(150,60)
$Permissions.Text = "Permissions"
$Permissions.Cursor = [System.Windows.Forms.Cursors]::Hand
$Permissions.Add_Click({Get-Permissions})
$groupBox.Controls.Add($Permissions)
#### Close ###################################################################
$Close = New-Object System.Windows.Forms.Button
$Close.Location = New-Object System.Drawing.Size(15,100)
$Close.Size = New-Object System.Drawing.Size(150,60)
$Close.Text = "Close"
$Close.Cursor = [System.Windows.Forms.Cursors]::Hand
$Close.Add_Click({$Form.Close()})
$groupBox.Controls.Add($Close)
###################### END BUTTONS ######################################################
#### Output Box Field ###############################################################
$outputBox = New-Object System.Windows.Forms.RichTextBox
$outputBox.Location = New-Object System.Drawing.Size(200,20)
$outputBox.Size = New-Object System.Drawing.Size(900,265)
$outputBox.Font = New-Object System.Drawing.Font("Consolas", 8 ,[System.Drawing.FontStyle]::Regular)
$outputBox.MultiLine = $True
$outputBox.ScrollBars = "Vertical"
$Form.Controls.Add($outputBox)
##############################################
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()
# destroy the form from memory
$Form.Dispose()

Powershell GUI tabbed layout script placement

I wrote six small gui scripts that I'd like to place in a single file, for the sake of converting said file into rxecutable with ps2exe.
I've found a script ,here on stack that is perfect for what I want. Unfortunatelly I cann't find any info on script placement within tabs and MS documentation leads me to ISE tabs, which is not helpfull.
Say I'd like to place this
Add-Type -AssemblyName PresentationFramework
$button_click = {
$folder = $textBox1.Text;
$pytanie = [System.Windows.MessageBox]::Show('Czy chcesz usunac folder?', '', '4');
If($pytanie -eq 'Yes')
{Remove-Item –path $folder –recurse -Force};
$test = Test-Path $folder;
if ($test -eq $false){[System.Windows.MessageBox]::Show('Folder Usuniety', '', '0')}}
$label2 = New-Object System.Windows.Forms.Label
$label2.AutoSize = $True
$label2.Text = ("Scieżka")
$label2.Location = New-Object System.Drawing.Point (10,30)
$label2.Size = New-Object System.Drawing.Size (25,70)
$label2.Font = [System.Drawing.Font]::new("Arial", 10, [System.Drawing.FontStyle]::Bold)
$textBox1 = New-Object System.Windows.Forms.TextBox
$textBox1.Location = New-Object System.Drawing.Point(10,70) ### Location of the text box
$textBox1.Size = New-Object System.Drawing.Size(200,50) ### Size of the text box
$textBox1.Multiline = $false ### Allows multiple lines of data
$textBox1.Font = New-Object System.Drawing.Font("Consolas",10,[System.Drawing.FontStyle]::Regular)
$textBox1.ReadOnly=$false
$button = New-Object System.Windows.Forms.Button
$button.Location = New-Object System.Drawing.Point(10,120)
$button.Size = New-Object System.Drawing.Size (200,30)
$button.Text = "Usun Folder"
$button.Add_Click($button_click)
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Mapowanie' ### Text to be displayed in the title
$form.Size = New-Object System.Drawing.Size(240,200) ### Size of the window
$form.StartPosition = 'Manual'
$form.Location = '10,10'
$form.Topmost = $true ### Optional - Opens on top of other windows
$form.Controls.AddRange(#($textBox1,$button, $label2))
$form.ShowDialog()
within a tab. How to do it?
I think that it the
$Tab1.Controls.Add($button)
You are looking for.
For example
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$ApplicationForm = New-Object System.Windows.Forms.Form
$ApplicationForm.StartPosition = "CenterScreen"
$ApplicationForm.Topmost = $false
$ApplicationForm.Size = "800,600"
$FormTabControl = New-object System.Windows.Forms.TabControl
$FormTabControl.Size = "755,475"
$FormTabControl.Location = "25,75"
$ApplicationForm.Controls.Add($FormTabControl)
$Tab1 = New-object System.Windows.Forms.Tabpage
$Tab1.DataBindings.DefaultDataSourceUpdateMode = 0
$Tab1.UseVisualStyleBackColor = $True
$Tab1.Name = "Tab1"
$Tab1.Text = "Tab1”
$FormTabControl.Controls.Add($Tab1)
$textBox1 = New-Object System.Windows.Forms.TextBox
$textBox1.Location = New-Object System.Drawing.Point(10,70) ### Location of the text box
$textBox1.Size = New-Object System.Drawing.Size(200,50) ### Size of the text box
$textBox1.Multiline = $false ### Allows multiple lines of data
$textBox1.Font = New-Object System.Drawing.Font("Consolas",10,[System.Drawing.FontStyle]::Regular)
$textBox1.ReadOnly=$false
$Tab1.Controls.Add($textBox1)
$Tab2 = New-object System.Windows.Forms.Tabpage
$Tab2.DataBindings.DefaultDataSourceUpdateMode = 0
$Tab2.UseVisualStyleBackColor = $True
$Tab2.Name = "Tab2"
$Tab2.Text = "Tab2”
$FormTabControl.Controls.Add($Tab2)
$button = New-Object System.Windows.Forms.Button
$button.Location = New-Object System.Drawing.Point(10,120)
$button.Size = New-Object System.Drawing.Size (200,30)
$button.Text = "Usun Folder"
$button.Add_Click($button_click)
$Tab2.Controls.Add($button)
# Initlize the form
$ApplicationForm.Add_Shown({$ApplicationForm.Activate()})
[void] $ApplicationForm.ShowDialog()

Powershell Script to get software Version of Adobe Connect & Zoom with GUI Error

Working on Powershell script to get adobe connect and Zoom Version in GUI. and getting hyperlink displayed. Any help will be highly appreciated.
Issue 1 - Both software version is not visible. Only one getting displayed either zoom or Adobe in output
Issue 2 - Unable to get Hyperlink for both row. Getting only for one at a time.
$ErrorActionPreference = "SilentlyContinue"
Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Drawing
$font = New-Object System.Drawing.Font("Arial", 9)
$form = New-Object System.Windows.Forms.Form
$form.Text = 'PLT Readiness'
$form.Size = New-Object System.Drawing.Size(325,225)
$form.StartPosition = 'CenterScreen'
$form.Icon = 'C:\Users\C899414\Desktop.ico'
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(120,150)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = 'OK'
$OKButton.TextAlign = 'MiddleCenter'
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)
$LinkLabel = New-Object System.Windows.Forms.LinkLabel
$LinkLabel.Location = New-Object System.Drawing.Point(81,110)
$LinkLabel.Size = New-Object System.Drawing.Size(280,20)
$LinkLabel.Font = $font
$LinkLabel.LinkColor = "BLUE"
$LinkLabel.ActiveLinkColor = "RED"
$LinkLabel.Text = "Ticketsystem"
$LinkLabel.add_Click({[system.Diagnostics.Process]::start("https://service-now.com/nav_to.do?uri=%2Fincident.do%3Fsys_id%3D-1%26sysparm_query%3Dactive%3Dtrue%26sysparm_stack%3Dincident_list.do%3Fsysparm_query%3Dactive%3Dtrue")})
$Form.Controls.Add($LinkLabel)
$software = New-Object System.Windows.Forms.Label $software.Location = New-Object System.Drawing.Point(10,50) $software.Size = New-Object System.Drawing.Size(280,20) $software.Font = $font $software.Text = "Adobe Connect: $((Get-WMIObject Win32_Product | Where-Object {$_.Name -like 'Adobe Connect Application*'} | Select-Object Version))" $software.Text = "Zoom: $((Get-WMIObject Win32_Product | Where-Object {$_.Name -like 'Zoom*'} | Select-Object Version))" $form.Controls.Add($software)
$help = New-Object System.Windows.Forms.Label
$help.Location = New-Object System.Drawing.Point(10,110)
$help.Font = $font
$help.Size = New-Object System.Drawing.Size(280,20)
$help.Text = "Help:"
$form.Controls.Add($help)
$help2 = New-Object System.Windows.Forms.Label
$help2.Location = New-Object System.Drawing.Point(10,130)
$help2.Font = $font
$help2.Size = New-Object System.Drawing.Size(280,20)
$help2.Text = "Update From Software Center:"
$form.Controls.Add($help)
$form.Topmst = $true
$form.ShowDialog()

form with two columns- text box get cut in the middle

im creating a gui script for users to get certain files, its not finished yet but i have a problem i cant solve.
i want to have choices of which file they want + an option for them to insert a name that they want for the file that they chose.
the problem is the text box is cut in the middle and only if you write a lot you will see the end of your text.
please help! thanks a lot!
this is the full script: *again-not complete
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
#This creates the path for the Json and also check if it is already there.
If(!(Test-Path -Path C:\Users\$env:USERNAME\documents\Json))
{
New-Item c:\users\$env:USERNAME\documents -ItemType directory -Name Json
$path = "c:\users\$env:USERNAME\documents\Json"
}
#creating the form
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Ofir`s script"
$objForm.Size = New-Object System.Drawing.Size(480,200)
$objForm.StartPosition = "CenterScreen"
#creating the label
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(20,20)
$objLabel.Size = New-Object System.Drawing.Size(280,20)
$objLabel.Text = "Please check the relevant boxes:"
$objForm.Controls.Add($objLabel)
#This creates a checkbox called dsp.z
$objDspCheckbox = New-Object System.Windows.Forms.Checkbox
$objDspCheckbox.Location = New-Object System.Drawing.Size(20,40)
$objDspCheckbox.Size = New-Object System.Drawing.Size(280,20)
$objDspCheckbox.Text = "dsp.z"
$objDspCheckbox.TabIndex = 0
$objForm.Controls.Add($objDspCheckbox)
#This creates a checkbox called fpga.bin
$objFpgaCheckbox = New-Object System.Windows.Forms.Checkbox
$objFpgaCheckbox.Location = New-Object System.Drawing.Size(20,60)
$objFpgaCheckbox.Size = New-Object System.Drawing.Size(280,20)
$objFpgaCheckbox.Text = "fpga.bin"
$objFpgaCheckbox.TabIndex = 1
$objForm.Controls.Add($objFpgaCheckbox)
#This creates a checkbox called bootrom_uncmp.bin
$objBootCheckbox = New-Object System.Windows.Forms.Checkbox
$objBootCheckbox.Location = New-Object System.Drawing.Size(20,80)
$objBootCheckbox.Size = New-Object System.Drawing.Size(280,20)
$objBootCheckbox.Text = "bootrom_uncmp.bin"
$objBootCheckbox.TabIndex = 2
$objForm.Controls.Add($objBootCheckbox)
#This creates a label for the TextBox1
$objLabel1 = New-Object System.Windows.Forms.Label
$objLabel1.Location = New-Object System.Drawing.Size(300,20)
$objLabel1.Size = New-Object System.Drawing.Size(280,20)
$objLabel1.Text = "Change the name?:"
$objForm.Controls.Add($objLabel1)
#This creates the TextBox1
$objTextBox1 = New-Object System.Windows.Forms.TextBox
$objTextBox1.Location = New-Object System.Drawing.Size(200,40)
$objTextBox1.Size = New-Object System.Drawing.Size(200,20)
$objTextBox1.TabIndex = 3
$objForm.Controls.Add($objTextBox1)
#ok Button
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(40,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click(
{
if ($objDspCheckbox.Checked -eq $true)
{
New-Item $path -itemtype file -name Dsp.json -value #"
"{"sys_ver":"01.01.01.02","RED":[],"RED_VA":[],"BLACK": [{"type":"6","type_descr":"DSP file","tar_name":"dsp.tar.gz","image_name":"dsp.z","CRC":"1234567","version":"01.01.01.02","metadata":"62056"}
],"BLACK_VA":[]
}"
"# ;$objForm.close()
}
elseif ($objFpgaCheckbox.Checked -eq $true)
{
New-Item $path -itemtype file -name Fpga.json -value #"
"
{"type":"4","type_descr":"FPGA file","tar_name":"FPGA.tar.gz","image_name":"fpga.z","CRC":"1234567","version":"01.01.01.02","metadata":"9730652"},
"
"#
;$objForm.close()
}
elseif ($objBootCheckbox.Checked -eq $true)
{
New-Item $path -itemtype file -name Boot.json -value "Hello3" ;$objForm.close()
}
})
$objForm.Controls.Add($OKButton)
#cancle Button
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(140,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)
#Parameters (Need to add)
$dspname = $objTextBox1
#makes the form appear on top of the screen
$objForm.Topmost = $True
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
the problem is the text box is cut in the middle and only if you write a lot you will see the end of your text. please help! thanks a lot!
The problem is that the TextBox control is hidden underneath your CheckBox controls, because you've configured them to be ridiculously wide.
Change:
$objDspCheckbox.Size = New-Object System.Drawing.Size(280,20)
to something more sensible, like 150px instead of 280px:
$objDspCheckbox.Size = New-Object System.Drawing.Size(150,20)
Do this for all the checkboxes and you'll find the problem goes away.
Alternatively, bring the $objTextBox1 control to the front by setting a ChildIndex of 0 on it's parent (the Form object itself):
$objForm.Controls.SetChildIndex($objTextBox1,0)
after adding all child controls (but before calling $objForm.ShowDialog())

POWERSHELL ISE Bulk/Batch select for encoding conversion (Get-Content & Set-Content)

POWERSHELL ISE
Hi, I'm trying to write a program that will grab all .txt files in an input path, change the encoding, and send the new .txt files to an output location.
I have it working based on single text boxes. I'm not exactly sure how to set it where it grabs all .txt files instead of one. (maybe an array?) And output with same file name in a new location.
My question is how can I write this to grab all .txt files and output them all to a new location instead of doing a single file at a time?
Here is my current code:
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$form = New-Object System.Windows.Forms.Form
$form.Text = "Text Converter"
$form.Size = New-Object System.Drawing.Size(300,300)
$form.StartPosition = "CenterScreen"
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(55,230)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(150,230)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)
$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 = "Input Path:"
$form.Controls.Add($label)
$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)
$form.Controls.Add($textBox)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,65)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = "Input Encoding:"
$form.Controls.Add($label)
$textBox2 = New-Object System.Windows.Forms.TextBox
$textBox2.Location = New-Object System.Drawing.Point(10,85)
$textBox2.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox2)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,115)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = "Output Path:"
$form.Controls.Add($label)
$textBox3 = New-Object System.Windows.Forms.TextBox
$textBox3.Location = New-Object System.Drawing.Point(10,140)
$textBox3.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox3)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,170)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = "Output Encoding"
$form.Controls.Add($label)
$textBox4 = New-Object System.Windows.Forms.TextBox
$textBox4.Location = New-Object System.Drawing.Point(10,190)
$textBox4.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox4)
$form.Topmost = $True
$form.Add_Shown({$textBox.Select()})
$result = $form.ShowDialog()
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
$x = $textBox.Text
$x
$x2 = $textBox2.Text
$x2
$x3 = $textBox3.Text
$x3
$x4 = $textBox4.Text
$x4
}
Get-Content $x -encoding $x2 |
Set-Content $x3 -encoding $x4
If you want the user to select multiple files, you could use an OpenFileDialog control to allow the user to select multiple input files, and then add them to a ListBox rather than a TextBox:
# Use a ListBox, since we're going to keep track of multiple strings
$InputFileBox = New-Object System.Windows.Forms.ListBox
$InputFileBox.Location = New-Object System.Drawing.Point -ArgumentList 10,10
$InputFileBox.Size = New-Object System.Drawing.Size -ArgumentList 265,240
$Form.Controls.Add($InputFileBox)
# Set up the "OpenFile" dialog, set the Multiselect property
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.InitialDirectory = 'D:\test\forms'
$OpenFileDialog.Multiselect = $true
$OpenFileButton = New-Object System.Windows.Forms.Button
$OpenFileButton.Location = New-Object System.Drawing.Point -ArgumentList 220,265
$OpenFileButton.Size = New-Object System.Drawing.Size -ArgumentList 55,20
$OpenFileButton.Text = 'Browse!'
$OpenFileButton.add_Click({
if($OpenFileDialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK)
{
foreach($FileName in $OpenFileDialog.FileNames)
{
# only add files not already in the list
if(-not $InputFileBox.Items.Contains($FileName))
{
$InputFileBox.Items.Add($FileName)
}
}
}
})
$Form.Controls.Add($OpenFileButton)
You can then grab the files names either directly from the $OpenFileDialog.FileNames or from the ListBox if you want to allow the user to edit the list:
$FileNames = [string[]]$InputFileBox.Items
For the output directory, use a FolderBrowserDialog control:
# TextBox is fine here, but disable it so users can't type directly in it
$OutputFolderTextBox = New-Object System.Windows.Forms.TextBox
$OutputFolderTextBox.Location = New-Object System.Drawing.Point -ArgumentList 10,10
$OutputFolderTextBox.Size = New-Object System.Drawing.Size -ArgumentList 265,20
$OutputFolderTextBox.Enabled = $false
$OutputFolderTextBox.BackColor = [System.Drawing.Color]::White
$Form.Controls.Add($OutputFolderTextBox)
# set up FolderBrowserDialog control
$OutputFolderBrowserDialog = New-Object System.Windows.Forms.FolderBrowserDialog
$OutputFolderBrowserDialog.RootFolder = [System.Environment+SpecialFolder]::MyComputer
$OutputFolderBrowseButton = New-Object System.Windows.Forms.Button
$OutputFolderBrowseButton.Location = New-Object System.Drawing.Point -ArgumentList 220,40
$OutputFolderBrowseButton.Size = New-Object System.Drawing.Size -ArgumentList 55,20
$OutputFolderBrowseButton.Text = 'Browse!'
$OutputFolderBrowseButton.add_Click({
if($OutputFolderBrowserDialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK)
{
# grab the selected folder path
$OutputFolderTextBox.Text = $OutputFolderBrowserDialog.SelectedPath
}
})
$Form.Controls.Add($OutputFolderBrowseButton)