Cheers all:
A one liner to the rescue:
# use one line to add a user to a local group
([ADSI]"WinNT://$computer/$Group,group").psbase.Invoke("Add",([ADSI]"WinNT://$domain/$user").path)
#
#
Of course we can cheet a bit and write function all in one line(it is not really a one-liner;) )
# a function all on one line
function Add-LocalUser{Param($computer=$env:computername,$group='Guests',$userdomain=$userdomain,$useraccount=$username)([ADSI]"WinNT://$computer/$Group,group").psbase.Invoke("Add",([ADSI]"WinNT://$domain/$user").path)}
#
#
#
Of course making this readable is always preferred.
function Add-LocalUser{
Param(
$computer=$env:computername,
$group='Guests',
$userdomain=$env:userdomain,
$username=$env:username
)
([ADSI]"WinNT://$computer/$Group,group").psbase.Invoke("Add",([ADSI]"WinNT://$domain/$user").path)
}
It is also really easy now to add help. Just add one single line and you will get:
help Add-LocalUser
Add-LocalUser -?
-debug
-verbose,
-erroraction,
-errorvriable,
-warningaction,
-warningvariable,
-outvariable,
-outbuffer
And much much more.
All this for one very simple line of code added to a function. (?one line?)