I have a PowerShell script that contains several functions. One of the functions should receive command line arguments. Here's a similar script to what I'm doing:
$mydocs = [Environment]::GetFolderPath("MyDocuments")
function example() {
Param(
[string]$name = "testfolder",
[switch]$foo = $false
)
$newdir = $mydocs + "\" + $name
if (!(Test-Path $newdir)) { mkdir $newdir }
}
example
So when I run this:
.\example.ps1 -name anewfolder -foo
I want the function example to use these arguments.
Because of scoping, my function has no $args. I put Write-Host $args into the function to double check, and it returns nothing. How can I pull the parent args, pass them into params, and get example to run with my arguments? Or should I just remove the function and run this part of my code in the parent scope?
param()set to the top of your script and then call the function (as you are doing) with the params specified.