# File Path Parameter

**URL:** <https://forums.powershell.org/t/file-path-parameter/8940>\
**Category:** PowerShell Help\
**Created:** [July 2, 2017, 1:57pm UTC](https://forums.powershell.org/t/file-path-parameter/8940 "2017-07-02T13:57:29Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![brian-clanton](https://avatars.discourse-cdn.com/v4/letter/b/ebca7d/32.png) [@brian-clanton](https://forums.powershell.org/u/brian-clanton)\
**Post date:** [July 2, 2017, 1:57pm UTC](https://forums.powershell.org/t/file-path-parameter/8940/1 "2017-07-02T13:57:29Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![bencamareno](https://avatars.discourse-cdn.com/v4/letter/b/e36b37/32.png) [@bencamareno](https://forums.powershell.org/u/bencamareno)\
**Post date:** [July 2, 2017, 2:52pm UTC](https://forums.powershell.org/t/file-path-parameter/8940/2 "2017-07-02T14:52:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![random-commandline](https://avatars.discourse-cdn.com/v4/letter/r/e56c9b/32.png) [@random-commandline](https://forums.powershell.org/u/random-commandline)\
**Post date:** [July 2, 2017, 3:50pm UTC](https://forums.powershell.org/t/file-path-parameter/8940/3 "2017-07-02T15:50:06Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![brian-clanton](https://avatars.discourse-cdn.com/v4/letter/b/ebca7d/32.png) [@brian-clanton](https://forums.powershell.org/u/brian-clanton)\
**Post date:** [July 2, 2017, 5:27pm UTC](https://forums.powershell.org/t/file-path-parameter/8940/4 "2017-07-02T17:27:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [July 2, 2017, 6:34pm UTC](https://forums.powershell.org/t/file-path-parameter/8940/5 "2017-07-02T18:34:40Z")

</div>

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/](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.

---

<div class="post-metadata">

**Author:** ![curtis-smith](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/curtis-smith/32/121_2.png) [@curtis-smith](https://forums.powershell.org/u/curtis-smith)\
**Post date:** [July 3, 2017, 1:18am UTC](https://forums.powershell.org/t/file-path-parameter/8940/6 "2017-07-03T01:18:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![brian-clanton](https://avatars.discourse-cdn.com/v4/letter/b/ebca7d/32.png) [@brian-clanton](https://forums.powershell.org/u/brian-clanton)\
**Post date:** [July 3, 2017, 11:02am UTC](https://forums.powershell.org/t/file-path-parameter/8940/7 "2017-07-03T11:02:27Z")

</div>

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
    }
}
```

---

<div class="post-metadata">

**Author:** ![curtis-smith](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/curtis-smith/32/121_2.png) [@curtis-smith](https://forums.powershell.org/u/curtis-smith)\
**Post date:** [July 5, 2017, 12:38am UTC](https://forums.powershell.org/t/file-path-parameter/8940/8 "2017-07-05T00:38:57Z")

</div>

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"
    }
```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:38pm UTC](https://forums.powershell.org/t/file-path-parameter/8940/9 "2024-05-16T20:38:25Z")

</div>

Closed on May 16, 2024, 8:38pm UTC


