# Creating a filename parameter

**URL:** <https://forums.powershell.org/t/creating-a-filename-parameter/923>\
**Category:** PowerShell Help\
**Created:** [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/creating-a-filename-parameter/923 "2011-12-31T18:30:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![archives](https://avatars.discourse-cdn.com/v4/letter/a/bb73d2/32.png) [@archives](https://forums.powershell.org/u/archives)\
**Post date:** [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/creating-a-filename-parameter/923/1 "2011-12-31T18:30:00Z")

</div>

by russelljspicer at 2013-04-30 07:01:14

> Looking to create a parameter for naming a file that will be used as a list of computers to run a couple of functions against.  
>   
> heres the code i have so far   
>   
> _Function NmCmptrLst {_  
>   
> _Param(  
> [Parameter(Mandatory=$True)]  
> [string]$FileName  
> )_  
>   
> _$computerlist = $FileName_  
>   
> _}_  
>   
> I think my issue is creating a variable that is a variable, just cant think of how i would change it…  
>   
> it is going to run in this portion of the script body:  
>   
> _NmCmptrLst_  
>   
> _GetComputers \>\> $computerlist_  
>   
> _$arrComputers = get-Content -Path $computerlist  
> foreach ($strComputer in $arrComputers)_  
>   
> any ideas?

by DonJ at 2013-04-30 07:54:29

> Please note that the CODE button, rather than the italics button, will format your code here in the forums.  
>   
> If you’re going to use the [Parameter()] attribute, you should add [CmdletBinding()] also:  
>   
> `function foo { [CmdletBinding()] Param(...)}`  
>   
> If your goal is to read the contents of the file:  
>   
> `$Computers = Get-Content $filenameforeach ($computer in $computers) {}`  
>   
> All your code is currently doing is putting the filename into the $computerlist variable, which isn’t necessary.

by russelljspicer at 2013-04-30 08:28:37

> Thanks for the response, I don’t think i explained this properly. What my code is doing(or attempting to) is to create a filename for this computer list that is generated by querying AD. Here is more:  
>   
>   
> So here i am trying to create a parameter that will let whoever runs the script input the filename  
> `Function NmCmptrLst {Param([Parameter(Mandatory=$True)][string]$FileName)$computerlist = "h:\myscripts$FileName"}`  
>   
> Once that file name is created i have this code that queries AD to create the actual file:  
>   
> `Function GetComputers {Param([Parameter(Mandatory=$True)][string]$OrgU)$ADsPath = [ADSI]â€œLDAP://OU=$OrgU,OU=Workstations,DC=XXXX,DC=XXXX,DC=ORGâ€$Search = New-Object DirectoryServices.DirectorySearcher($ADsPath)$Search.filter = â€œ(objectClass=computer)â€$Search.PageSize = 1000$Search.SearchScope = â€œSubTreeâ€$results = $Search.Findall()Foreach($result in $results){$computer = $result.GetDirectoryEntry()$computer.Name}}`  
>   
>   
> Once that file is created it is then used against the other functions:  
>   
> `$arrComputers = get-Content -Path $computerlistforeach ($strComputer in $arrComputers)`  
>   
> Sorry for not being more complete earlier. I hope this clarifies. BTW I am really new to this so I appreciate any help you give me.

by DonJ at 2013-04-30 08:40:55

> Uh… okay. I’m still not sure exactly what the question is. What isn’t working? It’s a bit unusual to create a function whose sole purpose is to add a path to a filename… seems over-modularized, but if that’s what you want, sure.

by russelljspicer at 2013-04-30 09:00:57

> I like to make it that way so its easier to add the same function to another script and keep the actual script body very efficient.  
>   
> So my problem is that when i run this it does not create a file…  
>   
> also i didnt add in what the script body is:  
>   
> `NmCmptrLstGetComputers >> $computerlist$arrComputers = get-Content -Path $computerlistforeach ($strComputer in $arrComputers)`  
>   
> it finishes without error but does not create a file.

by DonJ at 2013-04-30 09:19:05

> Ah. OK. In this function:  
>   
> `Function NmCmptrLst {Param([Parameter(Mandatory=$True)][string]$FileName)$computerlist = "h:\myscripts$FileName"}`  
>   
> You are not producing any output.  
>   
> `Function NmCmptrLst {Param([Parameter(Mandatory=$True)][string]$FileName)$computerlist = "h:\myscripts$FileName"Write-Output $computerlist}`  
>   
> Will output $computerlist as the result of the function. $computerlist remains defined only inside that function - $computerlist is not accessible outside the function. Read "about\_scope" in PowerShell’s help system for details. So the function has to output its result, and you have to capture that into a variable.  
>   
> Also, in your example, you’re just running NmCmptrLst - you’re not providing it with a filename parameter. It’ll prompt for one, though.  
>   
> `$myfilename = NmCmptrLst -filename whatever.txtGetComputers | Out-File $myfilename`

by russelljspicer at 2013-04-30 10:11:26

> Awesome!  
>   
> That worked perfectly except i removed the filename parameter because it was just naming the file "whatever.txt" when removed it prompted me to write one.  
>   
> BTW thanks for all of your patience, this forum is a wealth of knowledge!

by DonJ at 2013-04-30 10:43:34

> Well, you’d use the -filename parameter to specify the filename YOU wanted, rather than having to be PROMPTED for it. That’s kind of the point of the parameter.

---

<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, 9:03pm UTC](https://forums.powershell.org/t/creating-a-filename-parameter/923/2 "2024-05-16T21:03:54Z")

</div>

Closed on May 16, 2024, 9:03pm UTC


