Still really new to PS and scripting in general but I have been having a lot of fun learning over the last few months. I have a small issue when piping properties to Select-Object and I can't figure out what I am doing wrong, though I'm sure it's simple. Any insight is greatly appreciated!
This is the snippet I am having trouble with, I understand that using Read-Host is considered bad form, and I would prefer to build this out into a function with parameters, but this is how the folks who sign off on this want it so the dude abides.
$Target=Read-host"Enter the SamAccountName of the user you want to transfer"
$Verify=Get-ADUser-Identity$Target-Properties*|Select-Object-ExpandPropertyDisplayName,SamAccountName,CanonicalName,Description
It gives a ParameterBindingException "Select-Object : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'ExpandProperty'" error.
I know Get-ADUser is passing Management.ADUser objects and based on the error -expandproperty wants a System.String, so then I tried piping it to Out-String first, before Select-Object to convert to System.String
$Verify=Get-ADUser-Identity$Target-Properties*|Out-String|Select-Object-ExpandPropertyDisplayName,SamAccountName,CanonicalName,Description
Still the same error! So then I decided I should confirm Out-String converted the objects to System.String
$Verify | Get-Member
And as expected, $Verify now contains System.String objects. So at this point I know the following to be true:
$Target=Read-host"Enter the SamAccountName of the user you want to transfer"
Gives me a System.String object
$Verify=Get-ADUser-Identity$Target-Properties*|Out-String
Gives me a System.String object
$Verify=Get-ADUser-Identity$Target-Properties*|Select-Object-ExpandPropertyDisplayName,SamAccountName,CanonicalName,Description
Gives me an error
$Verify=Get-ADUser-Identity$Target-Properties*|Out-String |Select-Object-ExpandPropertyDisplayName,SamAccountName,CanonicalName,Description
Gives me an error
However,
Get-ADUser-IdentityTest.User-Properties*|Select-Object-ExpandPropertyDisplayName,SamAccountName,CanonicalName,Description
returns the output I expect. Where is my disconnect? Thank you in advance for the help!!