0

I'm new to any form of scripting/coding and I'm trying to get a piece of PowerShell code to allow the ICT teachers at the school to have access to the students OneDrive's. The code is pretty much all there, except whenever a variable is called it seems to output in a strange format.

As an example, here is my declaration of the variabl $teachers

$teachers = Get-ADGroupMember -Identity "ICT_Teacher_Group" | ForEach-Object {
    Get-ADUser -Identity $_.SamAccountName | Select-Object -Property UserPrincipalName
}

When outputted via Write-Output $teachers I get this format:


UserPrincipalName                
-----------------                
teacher1@mydomain.co.uk 
teacher2@mydomain.co.uk 
teacher3@mydomain.co.uk 
teacher4@mydomain.co.uk   

Which includes the heading of 'UserPrincipalName', which I think it where the problem lies.

When combined with a bit more of the code, the output gets worse.

Full Code:

$teachers = Get-ADGroupMember -Identity "Teacher Security Group" | ForEach-Object {
    Get-ADUser -Identity $_.SamAccountName | Select-Object -Property UserPrincipalName
}
$students = Get-ADGroupMember -Identity "Student Security Group" | Select-Object SamAccountName

$(forEach ($student in $students){
    forEach ($teacher in $teachers){
        Write-Output "OneDrive for $student is now available to $teacher"
    }
}) | Out-File test.txt

Output file:

OneDrive for @{SamAccountName=student1} is now available to @{UserPrincipalName=teacher1@mydomain.co.uk}
OneDrive for @{SamAccountName=student1} is now available to @{UserPrincipalName=teacher2@mydomain.co.uk}
OneDrive for @{SamAccountName=student1} is now available to @{UserPrincipalName=teacher3@mydomain.co.uk}
OneDrive for @{SamAccountName=student1} is now available to @{UserPrincipalName=teacher4@mydomain.co.uk}
OneDrive for @{SamAccountName=student2} is now available to @{UserPrincipalName=teacher1@mydomain.co.uk}
OneDrive for @{SamAccountName=student2} is now available to @{UserPrincipalName=teacher2@mydomain.co.uk}
etc...

Can anyone explain why the variables are outputted in the format @{UserPrincipalName=teacher1@mydomain.co.uk} and how I can change this to simply output teacher1@mydomain.co.uk

As a novice, any and all help would be greatly appreciated!

  • 1
    Use `Select-Object -ExpandProperty...` instead – Santiago Squarzon Mar 17 '22 at 16:41
  • In short: [`Select-Object`](https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/select-object) (`select`) by default returns _a `[pscustomobject]` instance_ that has the _requested properties_ - even when you're only asking for a _single_ property. To get only that property's _value_, use `-ExpandProperty ` instead - see the [linked answer](https://stackoverflow.com/a/48809321/45375) for details and alternatives. – mklement0 Mar 17 '22 at 16:43
  • 1
    Thank you both, that has done it! I've only been pulling my hair out for the last 2 hours trying to figure this out. – AdminAtLunch Mar 17 '22 at 16:50

0 Answers0