Search code examples
powershellcsvactive-directorymicrosoft-file-explorer

PowerShell - Getting the user to select a path with File Explorer


So I'm currently making a PowerShell script to sit on our DC that will create users in Active Directory, It will use Functions to display a menu and execute commands and will allow single user creation or CSV creation.

I'm trying to make it as idiot proof as possible and remove as much human error as possible. What I would like to do is Prompt the User with a File Explorer window so that when the CSV function looks for the path of the CSV itself to import the user has opened it in explorer first, removing the need to type the complete file path.

Here is my CSV Function at present for reference:

function csv {
 $Path = read-host "Where is the CSV stored? Make sure it is in a Readable Location"
 # Here is where The user would be prompted for the path of the CSV
 $Users = Import-Csv -Path $Path            
 foreach ($User in $Users)            
 {            
 $Displayname = $User.'Firstname' + " " + $User.'Lastname'            
 $UserFirstname = $User.'Firstname'            
 $UserLastname = $User.'Lastname'            
 $OU = $User.'OU'            
 $SAM = $User.'SAM'            
 $UPN = $User.'Firstname' + "." + $User.'Lastname' + "@" + $User.'Maildomain'            
 $Description = $User.'Description'            
 $Password = $User.'Password'            
 New-ADUser -Name "$Displayname" -DisplayName "$Displayname" -SamAccountName $SAM -UserPrincipalName $UPN -GivenName "$UserFirstname" -Surname "$UserLastname" -Description "$Description" -AccountPassword (ConvertTo-SecureString $Password -AsPlainText -Force) -Enabled $true -Path "$OU" -ChangePasswordAtLogon $false –PasswordNeverExpires $true            
 }
}

So that's my problem, is this a good idea or would it just be better to edit the script with an updated path each time?

Thanks for the help on this one!

Dylan


Solution

  • I use the following function, callable as e.g., $FileName = Select-File -StartingFolder "C:\Users\Michael\Documents" -NameFilter "CSV Files (*.CSV)|*.CSV"

    function Select-File {
    
        [CmdletBinding()]
    
        param(
            [Parameter(ParameterSetName="Single")]
            [Parameter(ParameterSetName="Multi")]
            [Parameter(ParameterSetName="Save")]
            [string]$StartingFolder = [environment]::getfolderpath("mydocuments"),
    
            [Parameter(ParameterSetName="Single")]
            [Parameter(ParameterSetName="Multi")]
            [Parameter(ParameterSetName="Save")]
            [string]$NameFilter = "All Files (*.*)|*.*",
    
            [Parameter(ParameterSetName="Single")]
            [Parameter(ParameterSetName="Multi")]
            [Parameter(ParameterSetName="Save")]
            [switch]$AllowAnyExtension,
    
            [Parameter(Mandatory=$true,ParameterSetName="Save")]
            [switch]$Save,
    
            [Parameter(Mandatory=$true,ParameterSetName="Multi")]
            [Alias("Multi")]
            [switch]$AllowMulti
        )
    
        if ($Save) {
            $Dialog = New-Object -TypeName System.Windows.Forms.SaveFileDialog
        } else {
            $Dialog = New-Object -TypeName System.Windows.Forms.OpenFileDialog
            if ($AllowMulti) {
                $Dialog.Multiselect = $true
            }
        }
        if ($AllowAnyExtension) {
            $NameFilter = $NameFilter + "|All Files (*.*)|*.*"
        }
        $Dialog.Filter = $NameFilter
        $Dialog.InitialDirectory = $StartingFolder
        [void]($Dialog.ShowDialog())
        $Dialog.FileNames
    }