Need help to understand Powershell code.

Hi,

Is someone can tell me what is the difference between "Get-ADUser User -Properties UserPrincipalName | select SamAccountName,UserPrincipalName" and ""Get-ADUser User | select SamAccountName,UserPrincipalName" because I got same result when I export it to csv:

1)
----------------------------------------------------------------------------------------------------
$SamAccount = Import-Csv "C:\SamAccountName.csv"

$SamAccount | % {Get-ADUser $_.SamAccountName -Properties UserPrincipalName | select SamAccountName,UserPrincipalName

} | Export-csv "C:\UserPrincialName.csv" -NoTypeInformation
----------------------------------------------------------------------------------------------------

2)
----------------------------------------------------------------------------------------------------

$SamAccount = Import-Csv "C:\SamAccountName.csv"

$SamAccount | % {Get-ADUser $_.SamAccountName | select SamAccountName,UserPrincipalName

} | Export-csv "C:\UserPrincialName.csv" -NoTypeInformation
----------------------------------------------------------------------------------------------------
LVL 1
SAM2009Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

footechCommented:
In this case there's no difference because UserPrincipalName is in the set of default properties that are always retrieved by the Get-ADUser command.  So, specifying -properties UserPrincipalName is redundant here.  If some other properties were specified with the -properties parameter then you could see different output.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
footechCommented:
FYI, the default properties are:
DistinguishedName
Enabled          
GivenName        
Name            
ObjectClass      
ObjectGUID      
SamAccountName  
SID              
Surname          
UserPrincipalName
0
MaheshArchitectCommented:
if you need any non default properties as output such as employeenumber etc, you need to specify it with -Property switch, then only select command can incorporate it, otherwise output of select command will drop that property though you specify it
0
Challenges in Government Cyber Security

Has cyber security been a challenge in your government organization? Are you looking to improve your government's network security? Learn more about how to improve your government organization's security by viewing our on-demand webinar!

SAM2009Author Commented:
Ok but what the difference between -properties and -property?
0
footechCommented:
No difference here.  -Properties is the documented parameter, but -Property also works.  Often PowerShell will try to match a parameter with partial info, and sometimes alternate names for parameters are programmed in.  This is why the following are all the same:
export-csv -notypeinformation
export-csv -notype
export-csv -nti


There's no guarantee that you can interchange -Property and -Properties at will.  For other cmdlets, -Property is the documented parameter.  For example, you can use
New-Object psobject -Property @{ name = "something"}

Open in new window

but the below will give an error.
New-Object psobject -Properties @{ name = "something"}

Open in new window

1
SAM2009Author Commented:
Thanks all for your help!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.