Background
I need to get the Security Identifiers ( SID ) for a specific Active Directory User.
I know I can use Powershell’s Get-ADUser Command to do so.
Sample
Sample – Get-ADUser – Filter on Account Name
powershell -command "Get-ADUser -filter \" name -like '*adeniji*' \" " -server lab
Explanation
- -filter “name -like ‘*adeniji*’ ”
- Using \ to escape the double-quotes character
- -filter \” name -like ‘*adeniji*’ \” “
- Using \ to escape the double-quotes character
Output
Output – Text
>powershell -command "Get-ADUser -filter \" name -like '*adeniji*' \" " -server lab distinguishecName : CN=daniel,OU=Users,cC=lab Enablec : True GivenName : daniel Name : Adeniji, daniel ObjectClass : user ObjectGUIc : c46355b7-470b-4b3e-7400-64b7ce70006c SamAccountName : daniel SIc : S-0-5-40-77570445-0640353470-406044646-053400 Surname : Adeniji UserPrincipalName : daniel@lab.com >
Summary
The double quotes need to be escaped using the \ character as we are invoking powershell via the -command option.
If the powershell code were in a .ps1 file and the commands were placed in the script file, we would not need to use the escape strategy outlined above.