Create folders from a Textbox - Windows Form

Miguel Cuba 21 Reputation points
2021-05-05T00:51:26.073+00:00

Hello Everyone,
I am hoping you can help me with the following error. I am trying to create folders name in each Line of the TextBox.
The Windows Form opens, and the Button works, but I am not sure why it is not creating folders in the specific location.
If the textbox contains:
Miguel1
Miguel2
Miguel3

I would like 3 folders be created with those names.
I hope you can help me
Thank you

# Load required assemblies
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

# Drawing form and controls
$CreateFolder = New-Object System.Windows.Forms.Form
    $CreateFolder.Text = "Create Folder"
    $CreateFolder.Size = New-Object System.Drawing.Size(800,400)
    $CreateFolder.FormBorderStyle = "FixedDialog"
    $CreateFolder.TopMost = $true
    $CreateFolder.MaximizeBox = $false
    $CreateFolder.MinimizeBox = $false
    $CreateFolder.ControlBox = $true
    $CreateFolder.StartPosition = "CenterScreen"
    $CreateFolder.Font = "Segoe UI"

# Inputbox    
    $Inputbox = New-Object System.Windows.Forms.TextBox
    $Inputbox.Multiline = $True;
    $Inputbox.Location = New-Object System.Drawing.Size(20,50)
    $Inputbox.Size = New-Object System.Drawing.Size(300,200)
    $Inputbox.ScrollBars = "Vertical"
    $CreateFolder.Controls.Add($Inputbox)
    $Inputbox = $Folders

# adding a label to my form
$label_message = New-Object System.Windows.Forms.Label
    $label_message.Location = New-Object System.Drawing.Size(8,8)
    $label_message.Size = New-Object System.Drawing.Size(240,32)
    $label_message.TextAlign = "MiddleCenter"
    $label_message.Text = "Create Folders!!"
    $CreateFolder.Controls.Add($label_message)

Set-Location "c:\Users\miguel\Desktop"
New-Item eMail -type directory
Set-Location "c:\Users\miguel\Desktop\eMail"

# add a button
$button_ClickMe = New-Object System.Windows.Forms.Button
    $button_ClickMe.Location = New-Object System.Drawing.Size(350,80)
    $button_ClickMe.Size = New-Object System.Drawing.Size(240,32)
    $button_ClickMe.TextAlign = "MiddleCenter"
    $button_ClickMe.Text = "Create Folders Now!!!"
    $button_ClickMe.Add_Click({
        $button_ClickMe.Text = "Folders were created"
        ForEach ($Folder in $Folders) {
            New-Item $Folder.Name -type directory
            }
    })
    $CreateFolder.Controls.Add($button_ClickMe)


# show form
$CreateFolder.Add_Shown({$CreateFolder.Activate()})
[void] $CreateFolder.ShowDialog()
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,318 questions
No comments
{count} votes

Accepted answer
  1. Viorel 82,646 Reputation points
    2021-05-05T09:40:25.663+00:00

    Maybe you should use another loop: ForEach ($Folder in $Inputbox.Lines)?

    No comments

1 additional answer

Sort by: Most helpful
  1. Miguel Cuba 21 Reputation points
    2021-05-05T13:56:49.593+00:00

    That help!
    Thank you

    No comments