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?
 
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
 
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
On-Demand: Securing Your Wi-Fi for Summer Travel

Traveling this summer?Check out our on-demand webinar to learn about the importance of Wi-Fi security and 3 easy measures you can start taking immediately to protect your private data while using public Wi-Fi. Follow us today to learn more!

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.