_kstone Posted July 22, 2015 Posted July 22, 2015 Morning all, My Powershell knowledge is fairly basic. I have a little script that provides the user with a single text box. The data that they enter here is saved as $x I now want to create a CSV file that has the value of $x in it, but i'm struggling a little bit. Does anybody know how to do this? I imagine it would actually be fairly simple! Many thanks
pcstru Posted July 22, 2015 Posted July 22, 2015 Wrap it up as an object : $A = "Hello"; $ObjA = New-Object PSObject -Property @{ FirstColumn = $A } Export-Csv -InputObject $ObjA -Path C:\tmp\test.csv -NoTypeInformation
Sephiroth Posted July 22, 2015 Posted July 22, 2015 Is $x a variable or an array? Makes a difference to the answer!
_kstone Posted July 22, 2015 Author Posted July 22, 2015 Is $x a variable or an array? Makes a difference to the answer! You know as much as I do haha, here's the script [void] [system.Reflection.Assembly]::LoadWithPartialName("System.Drawing") [void] [system.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") $objForm = New-Object System.Windows.Forms.Form $objForm.Text = "Update Special Password" $objForm.Size = New-Object System.Drawing.Size(300,200) $objForm.StartPosition = "CenterScreen" $objForm.KeyPreview = $True $objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") {$x=$objTextBox.Text;$objForm.Close()}}) $objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") {$objForm.Close()}}) $OKButton = New-Object System.Windows.Forms.Button $OKButton.Location = New-Object System.Drawing.Size(75,120) $OKButton.Size = New-Object System.Drawing.Size(75,23) $OKButton.Text = "OK" $OKButton.Add_Click({$x=$objTextBox.Text;$objForm.Close()}) $objForm.Controls.Add($OKButton) $CancelButton = New-Object System.Windows.Forms.Button $CancelButton.Location = New-Object System.Drawing.Size(150,120) $CancelButton.Size = New-Object System.Drawing.Size(75,23) $CancelButton.Text = "Cancel" $CancelButton.Add_Click({$objForm.Close()}) $objForm.Controls.Add($CancelButton) $objLabel = New-Object System.Windows.Forms.Label $objLabel.Location = New-Object System.Drawing.Size(10,20) $objLabel.Size = New-Object System.Drawing.Size(280,20) $objLabel.Text = "Enter the new Special Password and press OK" $objForm.Controls.Add($objLabel) $objTextBox = New-Object System.Windows.Forms.TextBox $objTextBox.Location = New-Object System.Drawing.Size(10,40) $objTextBox.Size = New-Object System.Drawing.Size(260,20) $objForm.Controls.Add($objTextBox) $objForm.Topmost = $True $objForm.Add_Shown({$objForm.Activate()}) [void] $objForm.ShowDialog()
pcstru Posted July 22, 2015 Posted July 22, 2015 So you could add : $A=$ObjTextBox.Text $ObjA = New-Object PSObject -Property @{ FirstColumn = $A } Export-Csv -InputObject $ObjA -Path C:\tmp\test.csv -NoTypeInformation 1
_kstone Posted July 22, 2015 Author Posted July 22, 2015 So you could add : $A=$ObjTextBox.Text $ObjA = New-Object PSObject -Property @{ FirstColumn = $A } Export-Csv -InputObject $ObjA -Path C:\tmp\test.csv -NoTypeInformation Good stuff, all working. Thanks guys!
Sephiroth Posted July 22, 2015 Posted July 22, 2015 So you could add : $A=$ObjTextBox.Text $ObjA = New-Object PSObject -Property @{ FirstColumn = $A } Export-Csv -InputObject $ObjA -Path C:\tmp\test.csv -NoTypeInformation Yes. You beat me to the response both times!
halbaradkenafin Posted July 22, 2015 Posted July 22, 2015 Need to add -Append on to that as it will overwrite the csv file each time it writes to it.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now