Search code examples
powershellpowershell-4.0azure-powershell

Use a variable as parameter in Set-MsolUser


Why can't I use a variable as a parameter in PowerShell?

Set-MsolUser -UserPrincipalName [email protected] -$parameter Stockholm

$parameter is equal to City in this case

Set-MsolUser : A positional parameter cannot be found that accepts argument '-City'.
At line:1 char:1
+ Set-MsolUser -UserPrincipalName [email protected]
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Set-MsolUser], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.Online.Administration.Automation.SetUser

Solution

  • You could use the following options:

    1)Invoke the command with arguments

    Invoke-Command -ScriptBlock {Get-ChildItem} -ArgumentList "-$($para) C:\Temp"
    

    2) Use splatting

    $Val = 'Path'
    $HashArguments  = @{
        $Val = 'C:\Temp'
    }
    
    Get-ChildItem @HashArguments