Command line too long..

Welcome Forums General PowerShell Q&A Command line too long..

This topic contains 5 replies, has 2 voices, and was last updated by

 
Participant
4 years, 4 months ago.

  • Author
    Posts
  • #16151

    Participant
    Points: 0
    Rank: Member

    Hi all,

    Just wondering what technique I could use to shorten my command line other than using variables for each parameter..

    e.g.
    Set-QADUser -Identity $_."Name" -UserPrincipalName $_."UserPrincipalName" -alias $_."alias" -Displayname $_."Displayname" -FirstName $_."FirstName" -LastName $_."LastName" -OrganizationalUnit $_."OrganizationalUnit" -PrimarySmtpAddress $_."PrimarySmtpAddress" -SamAccountName $_."SamAccountName" -Password (ConvertTo-SecureString $_.password -AsPlainText -Force)

    Any ideas appreciated!

    Cheers,
    AKP

  • #16152

    Member
    Points: 0
    Rank: Member

    I prefer to use splatting to keep that sort of long command readable:

    $params = @{
        Identity           = $_.Name
        UserPrincipalName  = $_.UserPrincipalName
        Alias              = $_.Alias
        DisplayName        = $_.DisplayName
        FirstName          = $_.FirstName
        LastName           = $_.LastName
        OrganizationalUnit = $_.OrganizationalUnit
        PrimarySmtpAddress = $_.PrimarySmtpAddress
        SamAccountName     = $_.SamAccountName
        Password           = ConvertTo-SecureString $_.Password -AsPlainText -Force
    }
    
    Set-QADUser @params
    
  • #16153

    Participant
    Points: 0
    Rank: Member

    Thanks Dave, just what I was looking for...

    Are these hash tables?

  • #16154

    Member
    Points: 0
    Rank: Member

    Yep, $params is a hash table. When you splat a hashtable, its keys become the names of command Parameters, and its values are the arguments passed to those parameters.

    You can also do splatting with an array, in which case each element of the array is passed as a positional parameter, in the same order as the array.

  • #16155

    Participant
    Points: 0
    Rank: Member

    Thank you Dave.
    I will do some reading up on arrays as well.

  • #16178

    Participant
    Points: 0
    Rank: Member

    I know understand spaltting!

The topic ‘Command line too long..’ is closed to new replies.