File Path Parameter

I have composed a script that will create a spreadsheet based on Active Directory User Groups but I need to prompt the user for a Path so I know where to save the Excel sheet report it generates.

I can test the path name individually and then create the file if the path validates; however, I want to give the user the option of NOT putting in a path statement and just declaring a file name, then the script would just save it to the current working directory.

The only thing I can think of is to test to see if ANY back slashes are in the users input string, but I was wondering if anyone has used a different method.

Here is some ‘test’ commands I have been working with to see what I can accomplish:

Param
    (
        [Parameter(Mandatory=$true)]
        [string]
        $filePath
        
    )

Function TestPath($filpath){
    
    $ParentFolders = Split-Path -Path $filepath
    $File = Split-Path $filePath -Leaf
     if (Test-Path $ParentFolders){
        write-host "Create file $file or save full path to variable"
        New-Item $filepath
      }
      else
      {
        write-host "Invalid Path"
        Exit
      }
}

TestPath $filePath
Remove-Item $filePath
$ParentFolder = $null
$file = $null

You should have 2 params:
1 filepath
2 filename

The default of filepath $pwd which is the current working directory. So if the user doesn’t enters a path, the file will be saved in $pwd

Then just join filepath and filename.

At The end, write-output the full path where the file was saved.

You can export Active Directory objects to csv via Export-csv.

Get-ADUser | Select-Object Property1,Property2 | 
Export-Csv -NoTypeInformation -Path .\AD.csv # Or \\path\AD.csv

There is a lot of special formatting involved in the creation of the spreadsheet. We will submit these reports to our clients so they can use the form to fill out additional fields. Plus the script needs to have implicit emoting to domain controller so other technicians can run it.

Did you think about using the standard select folder dialog box of windows?

http://www.powershellmagazine.com/2013/06/28/pstip-using-the-system-windows-forms-folderbrowserdialog-class/

This way you could be sure that the path is valid.

I really don’t completely understand the challenge here. You can use Test-Path which can accept an absolute path or a relative path.

PS C:\Temp> Test-Path "file.csv"
Test-Path "c:\temp\file.csv"
True
True

So the user can specify either a full path, a relative path, or just a file name and it should be able to test it fine. Then, if for some reason you need the full path to the file after it is created, you can user resolve-path, or if you need to get the current directory location you can user get-location. So I would need to understand more about the challenge you are facing with not being able to specify a filename only.

To keep the code at a minimum I have the 'ValidateScript as part of my parameter list and a separate function to ensure that the technician enters a filename that has a correct file extension.

Param
    (
        [ValidateScript({Test-Path $_})] 
        [string]
        $filePath = $pwd,
        [Parameter(Mandatory=$true)]
        [string]
        $Client,
        [Parameter(Mandatory=$true)]
        $filename
            
    )

#Tests for Proper File extension
Function TestfileInput ($filename) {
    $extn = [IO.Path]::GetExtension($filename)
    if ($extn -eq ".xls" -or $extn -eq ".xlsx"){
        Write-Output "Valid Input"
    }
    else{
        Write-Warning "Filename must end in .xlsx extension Try again"
        Exit
    }
}

So if I’m understanding correctly, you are not wanting to check the existence of the file, but rather the parent folder where the file would be created. How about something like this?

    Param(
        [ValidateScript({Test-Path -Path (Split-Path -Path ([io.path]::GetFullPath($_)) -Parent)})]
        [string]
        $filePath,
        [Parameter(Mandatory=$true)]
        [string]
        $Client
    )
    if ([IO.Path]::GetExtension($filePath) -match ".xls|.xlsx") {
        Write-Output "Valid Input"
    } Else {
        Write-Warning "Filename must end in .xlsx extension Try again"
    }