Breaking long line of code in a script

How can i break up this line in a script to make it look neater ? I don’t want to use backtick’s if can…

Add-CMUserCollectionQueryMembershipRule -CollectionName "Name" -QueryExpression "select SMS_R_USER.ResourceID,SMS_R_USER.ResourceType,SMS_R_USER.Name,SMS_R_USER.UniqueUserName,SMS_R_USER.WindowsNTDomain from SMS_R_User where SMS_R_User.SecurityGroupName = 'groupname' " -RuleName "rulename" -Verbose

Would breaking up the QueryExpression like this work ok ?

$QE = ("select SMS_R_USER.ResourceID,SMS_R_USER.ResourceType,SMS_R_USER.Name,SMS_R_USER.UniqueUserName,SMS_R_USER.WindowsNTDomain from SMS_R_User 
where SMS_R_User.SecurityGroupName")

If you don’t want to use backtick, you could consider using splatting technique. Splatting is a method of passing a collection of parameter values to a command as unit. Windows PowerShell associates each value in the collection with a command.

So you can modify your code as -

$params = @{'Collection name' = 'Name'
            'QueryExpression' = 'select SMS_R_USER.ResourceID,SMS_R_USER.ResourceType,SMS_R_USER.Name,SMS_R_USER.UniqueUserName,SMS_R_USER.WindowsNTDomain from SMS_R_User where SMS_R_User.SecurityGroupName = "groupname"'
            'RuleName' = 'rulename'
           }

Add-CMUserCollectionQueryMembershipRule $params -verbose

@ is for splatting.

Add-CMUserCollectionQueryMembershipRule @params -verbose

Thanks again everyone.

Also look at here strings:

$groupname = "foo"

$hereString =@" 
    select SMS_R_USER.ResourceID,
           SMS_R_USER.ResourceType,
           SMS_R_USER.Name,
           SMS_R_USER.UniqueUserName,
           SMS_R_USER.WindowsNTDomain 
    From SMS_R_User 
    where SMS_R_User.SecurityGroupName = '$groupname'
"@

Thats nice Rob, like that. What is it called when you use @= ? Not an array is it ?

Graham,

What about this:

$groupName = 'foo'

$query = @"
'select SMS_R_USER.ResourceID,
            SMS_R_USER.ResourceType,
            SMS_R_USER.Name,SMS_R_USER.UniqueUserName,
            SMS_R_USER.WindowsNTDomain 
            from SMS_R_User where SMS_R_User.SecurityGroupName = "$groupName"'
"@

$params = @{
    'Collection name'	= 'Name'
    'QueryExpression'	= $query
    'RuleName'			= 'rulename'
}

Add-CMUserCollectionQueryMembershipRule @params -Verbose

I’m using splatting and a here-string construct, like GJ and Rob suggested.

Richard thats brilliant. Looks really nice and clean. Like that. Thanks again. (p.s ordered my "Learn Powershell toolmaking in a month of lunches Sunday, Richard. Looking to reading it :slight_smile: )