PowerShell Active Directory Script - What value or variable to use to get the correct output
I'm a newbie and trying to create a simple script to query a user's active directory username ( I have an AD lab) but can't seem to figure out which value or variable to use to retrieve the correct object. I believe the ($username) needs to be replaced with something but I'm just not sure what. This is my code:
Import-Module ActiveDirectory
read-host -prompt 'Input User ID'
get-aduser -identity ($username)
net user ($username)
get-adprincipalgroupmembership -identity ($username) | select -expand name
Solution 1:
Like @Am_I_Helpful stated, you have to capture the Read-Host
prompt inside the $username
variable.
Import-Module ActiveDirectory
$username = Read-Host -Prompt 'Input User ID'
Get-ADUser -Identity $username
NET USER $username
get-ADPrincipalGroupMembership -Identity $username | Select -Expand Name