none
How to add SaveFileDialog to PowerShell Get-ADUser Export-CSV RRS feed

  • Question

  • Hi,

    I am having a bit of difficulties with getting the SaveFileDialog to work when I use the Get-ADUser export-CSV.

    Current code:

    $handler_Output_Click= 
    {
    $User = Get-Aduser $textBox1.Text -Properties DisplayName,sAMAccountName,EmailAddress,Mobile,Company,Title,Enabled,LockedOut,Description,Created,Modified,LastLogonDate,AccountExpirationDate,AccountLockoutTime,BadLogonCount,CannotChangePassword,LastBadPasswordAttempt,PasswordLastSet,PasswordExpired,LogonWorkstations,CanonicalName | Select DisplayName,sAMAccountName,EmailAddress,Mobile,Company,Title,Enabled,LockedOut,Description,Created,Modified,LastLogonDate,AccountExpirationDate,AccountLockoutTime,BadLogonCount,CannotChangePassword,LastBadPasswordAttempt,PasswordLastSet,PasswordExpired,LogonWorkstations,CanonicalName | Export-CSV C:\temp\test.csv -NoTypeInformation ';' -encoding utf8
    $richTextBox1.Text = "A file 'test.csv' has been created to C:\temp\ based on the user: $($textBox1.Text)"
    }


    Here a specific filename is already defined and I have to edit the code each time I want a different filename.

    It would be perfect if I could implement the SaveFileDialog box so I have the ability to name the file before saving and possibly even have the option to select the file type (among .CSV and All files).

    This it the export/output button itself:

    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 502
    $System_Drawing_Point.Y = 38
    $Output.Location = $System_Drawing_Point
    $Output.Name = "Output"
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Height = 23
    $System_Drawing_Size.Width = 85
    $Output.Size = $System_Drawing_Size
    $Output.TabIndex = 2
    $Output.Text = "Export as file"
    $Output.UseVisualStyleBackColor = $True
    $Output.add_Click($handler_Output_Click)
    # $form1.AcceptButton = $Output
    $Output.DataBindings.DefaultDataSourceUpdateMode = 0
    $form1.Controls.Add($Output)

    And in the beginning of my script I also have defined the following:

    [System.Windows.Forms.Application]::EnableVisualStyles();
    [reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
    [reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
    [reflection.assembly]::loadwithpartialname("System.Windows.Forms.SaveFileDialog") | Out-Null

    And also:

    $Output = New-Object System.Windows.Forms.Button

    Any ideas how can I implement the SaveFileDialog so when I press the "Export as file" button the PowerShell command "Get-Aduser $textBox1.Text -Properties DisplayName,sAMAc..." is ran and I can choose the file name from a pop-up dialog box where to save the file and also put a filename? Currently I have to edit the code in order to assign a new file name (or go rename the file from that location).

    Thank you in advance,

    Henri


    EDIT:

    I know that the below is the answer to the SaveFileDialog, however I cannot imagine how I could implement it to my script into the "Get-Aduser $textBox1.Text -Properties a,b,c,d | select a,b,c,d | Export-CSV C:\temp\test.csv" cmdlet.

    Function Get-SaveFile($initialDirectory)
    { 
    [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
    Out-Null
    
    $SaveFileDialog = New-Object System.Windows.Forms.SaveFileDialog
    $SaveFileDialog.initialDirectory = $initialDirectory
    $SaveFileDialog.filter = "All files (*.*)| *.*"
    $SaveFileDialog.ShowDialog() | Out-Null
    $SaveFileDialog.filename
    }


    • Edited by mhenri2200 Saturday, May 2, 2015 12:53 AM
    Friday, May 1, 2015 11:59 PM

Answers

  • You have to use the dialog to get the name then save the file.  What did you think would happen?  The dialog does not know what you are trying to do.

    Just run this at a prompt to see how it works:

    Add-Type -AssemblyName System.Windows.Forms
        $dlg=New-Object System.Windows.Forms.SaveFileDialog
    
    if($dlg.ShowDialog() -eq 'Ok'){
        Write-host "you chose to create $($dlg.filename)"
    }


    \_(ツ)_/

    • Marked as answer by mhenri2200 Saturday, May 2, 2015 2:29 AM
    Saturday, May 2, 2015 12:38 AM
    Moderator
  • Just run the dialog before exporting the file.  Why is that a problem?

    \_(ツ)_/

    • Marked as answer by mhenri2200 Saturday, May 2, 2015 2:29 AM
    Saturday, May 2, 2015 1:49 AM
    Moderator

All replies

  • You have to use the dialog to get the name then save the file.  What did you think would happen?  The dialog does not know what you are trying to do.

    Just run this at a prompt to see how it works:

    Add-Type -AssemblyName System.Windows.Forms
        $dlg=New-Object System.Windows.Forms.SaveFileDialog
    
    if($dlg.ShowDialog() -eq 'Ok'){
        Write-host "you chose to create $($dlg.filename)"
    }


    \_(ツ)_/

    • Marked as answer by mhenri2200 Saturday, May 2, 2015 2:29 AM
    Saturday, May 2, 2015 12:38 AM
    Moderator
  • You have to use the dialog to get the name then save the file.  What did you think would happen?  The dialog does not know what you are trying to do.

    Just run this at a prompt to see how it works:

    Add-Type -AssemblyName System.Windows.Forms
        $dlg=New-Object System.Windows.Forms.SaveFileDialog
    
    if($dlg.ShowDialog() -eq 'Ok'){
        Write-host "you chose to create $($dlg.filename)"
    }


    \_(ツ)_/

    Thanks! This helped a lot.

    I tried it and it works quite well.

    Add-Type -AssemblyName System.Windows.Forms
        $dlg = New-Object System.Windows.Forms.SaveFileDialog
        $dlg.Filter = "CSV Files (*.csv)|*.csv|Text Files (*.txt)|*.txt|Excel Worksheet (*.xls)|*.xls|All Files (*.*)|*.*"
        $dlg.SupportMultiDottedExtensions = $true;
        $dlg.InitialDirectory = "C:\temp\"
        $dlg.CheckFileExists = $true;
    
    if($dlg.ShowDialog() -eq 'Ok'){
        Write-host "you chose to create $($dlg.filename)"
    }

    I know that the SaveFileDialog doesn't actually save the file, it just defines the location the user selected and possibly the file type.

    However, I cannot figure out how to merge the two so if I click on the "Export as file" button, I will get the dialog and be able to save it via the Get-ADUser export.

    Apologizes, I'm still a newbie to this stuff.




    • Edited by mhenri2200 Saturday, May 2, 2015 1:56 AM
    Saturday, May 2, 2015 1:30 AM
  • Just run the dialog before exporting the file.  Why is that a problem?

    \_(ツ)_/

    • Marked as answer by mhenri2200 Saturday, May 2, 2015 2:29 AM
    Saturday, May 2, 2015 1:49 AM
    Moderator
  • Just run the dialog before exporting the file.  Why is that a problem?

    \_(ツ)_/

    It works now! I made some modifications and it works. Thank you very much for the advice.

    $handler_Output_Click={
    Add-Type -AssemblyName System.Windows.Forms
        $SaveAs1 = New-Object System.Windows.Forms.SaveFileDialog
        $SaveAs1.Filter = "CSV Files (*.csv)|*.csv|Text Files (*.txt)|*.txt|Excel Worksheet (*.xls)|*.xls|All Files (*.*)|*.*"
        $SaveAs1.SupportMultiDottedExtensions = $true;
        $SaveAs1.InitialDirectory = "C:\temp\"
    
    if($SaveAs1.ShowDialog() -eq 'Ok'){
        $User = Get-Aduser $textBox1.Text -Properties DisplayName,sAMAccountName,EmailAddress,Mobile,Company,Title,Enabled,LockedOut,Description,Created,Modified,LastLogonDate,AccountExpirationDate,AccountLockoutTime,BadLogonCount,CannotChangePassword,LastBadPasswordAttempt,PasswordLastSet,PasswordExpired,LogonWorkstations,CanonicalName | Select DisplayName,sAMAccountName,EmailAddress,Mobile,Company,Title,Enabled,LockedOut,Description,Created,Modified,LastLogonDate,AccountExpirationDate,AccountLockoutTime,BadLogonCount,CannotChangePassword,LastBadPasswordAttempt,PasswordLastSet,PasswordExpired,LogonWorkstations,CanonicalName | Export-CSV $($SaveAs1.filename) -NoTypeInformation ';' -Encoding UTF8
        $richTextBox1.Text = "A file $($SaveAs1.filename) has been created based on the user: $($textBox1.Text)"
    }
    }

    • Edited by mhenri2200 Saturday, May 2, 2015 9:20 AM
    Saturday, May 2, 2015 2:28 AM
  • See how easy it is once you see how the bits work.


    \_(ツ)_/

    Saturday, May 2, 2015 2:46 AM
    Moderator
  • And why couldnt you just have given him the answer right away ?

    Sunday, August 20, 2017 11:49 AM