Q:

Using a variable in a PowerShell script

I'm trying to put a small collection of simple scripts together for PowerShell to make life easier but am having some problems with variables in these scripts.
In a Linux environment I would use a variable in my scripts (usually $1, $2, etc....) like this to make things easier
sed -i 's/$1/$2/g' filename.conf

So that all I would need to do is this
./findreplace.sh old new filename.conf

In powershell, I want to achieve similar, specifically with this command:
Get-ADPrincipalGroupMembership account.name | select name

In this case, the $1 would be where 'user.name' is, so that I would be doing:
 .\groups.ps1 user.name

Is there the facility for this?

A:

Groups.ps1 should have the following content:
param( $user )

Get-ADPrincipalGroupMembership $user | Select Name

You can then invoke it as shown, or as .\Groups.ps1 -user user.name.
However, I'd probably try to do it as an "advanced command", and allow it to handle multiple names, and also names passed in via the pipeline:
[CmdletBinding()]

param (
    [Parameter(ValueFromPipeline=$true;ValueFromPipelineByPropertyName=$true)]
    [Alias("Identity","sAMAccountName")
    string[] $Users
)

PROCESS {
    ForEach ($User in $Users) {
        Get-ADPrincipalGroupMembership $User | Select Name
    }
}

which would also allow you to do something like Get-ADUser | .\Groups.ps1

