When the script runs it gets all the right information but places each piece of information under each object member but not separated. It’s all mashed into one long string.
Добрый день, подскажите пожалуйста в чем ошибка, хочется из csv таблицы по столбцу ФамилияИмя найти пользователей AD по displayName и изменить у них описание на значения из таблицы столбца ИНН, код :
Is my syntax wrong: $($_.samaccountname)?
Get-ADUser -Filter * -SearchBase "OU=Test,OU=People,DC=ad,DC=domain,DC=com" | Set-ADUser -email "$($_.samaccountname)@domain.com"I appreciate the help.
asked Sep 29, 2023 at 23:11
The problem is not your syntax, but that the variable $_ doesn’t exist in this context.
To use it you need to be inside a foreach loop.
Get-ADUser -Filter * -SearchBase "OU=Test,OU=People,DC=ad,DC=domain,DC=com" | Foreach-Object { Set-ADUser -email "$($_.samaccountname)@domain.com" }answered Sep 30, 2023 at 5:34
Gerald Schneider
8 gold badges62 silver badges90 bronze badges
Set-ADUser -email "$($_.samaccountname)@domain.com"It’s quite simpler than that. Just use
Set-ADUser -email ($_.samaccountname + "@domain.com")answered Sep 29, 2023 at 23:28

58 gold badges211 silver badges333 bronze badges
Streamline Your Data Management with Sourcetable
When working with PowerShell samAccountName data, you might be accustomed to the traditional approach of exporting information to a CSV file and then importing it into a spreadsheet program. Here’s where Sourcetable transforms your workflow. With Sourcetable, you can directly import your PowerShell samAccountName into a dynamic, easy-to-use spreadsheet. This eliminates the need for the intermediate step of exporting to CSV, thus simplifying the process.
Using Sourcetable provides significant benefits over the conventional method. Firstly, Sourcetable syncs with your live data. This means that any changes in the PowerShell samAccountName are automatically updated in your spreadsheet, ensuring you always have the most current information at your fingertips. Moreover, Sourcetable’s ability to pull data from multiple sources into one interface allows for powerful automation and enhances your business intelligence capabilities. Its familiar spreadsheet format also means there’s no steep learning curve, allowing you to query and analyze your data with ease.
Common Use Cases
Use case 1: Generating a report of user account names and email addresses from a specific organizational unit (OU) for auditing purposes
Use case 2: Creating a backup of samAccountName and mail properties for users in a particular OU before performing mass updates or deletions
Use case 3: Exporting a list of samAccountName and mail properties to be used for cross-referencing or synchronizing with another system or directory service
Use case 4: Filtering and exporting user account details based on specific criteria for targeted user management operations
Use case 5: Importing a list of samAccountNames from a CSV to retrieve and export their full set of user properties for detailed analysis or record-keeping
Member Access Enumeration
Reading between the lines, I think you’re inadvertently using a PowerShell feature called Member Access Enumeration.
# set up some test data
$users = @( [pscustomobject] @{ "firstname" = "vic" "lastname" = "reeves" }, [pscustomobject] @{ "firstname" = "bob" "lastname" = "mortimer" }
);
# build the result object
$output = [pscustomobject] @{ "firstname" = $users.firstname "lastname" = $users.lastname "pcname" = "..."
}
# display the result
$output | format-tablefirstname lastname pcname
--------- -------- ------
{vic, bob} {reeves, mortimer} ...When these values are rendered onto the screen they get serialised as above.
Exporting samAccountName to a CSV File in PowerShell
Using Get-ADUser with Select-Object and Export-Csv
Example PowerShell Script
Filtering and Selecting Specific Properties
Pipeline
# loop over each account adding it to the $output array
$output = $users | foreach-object { if($_.SamAccountName -like "companyname-initials*") { $UserCpu = Get-ADComputer -Filter "Description -like 'companyname-initials*'" } else { $UserCpu = Get-ADComputer -Filter "Description -like '*$Name*'" } # this implicitly returns the values to the pipeline to be # collected into the $output variable as an array [pscustomobject]@{ Email = $_.UserPrincipalName Login = $_.SamAccountName PCName = $UserCpu.Name }
}Frequently Asked Questions
How can I export samAccountName and mail for users in a specific OU to a CSV file using PowerShell?
What does the -SearchBase parameter do in the Get-ADUser cmdlet?
Do I need to specify which properties to export when using Get-ADUser cmdlet?
How do I ensure that I export all properties of user objects using the Get-ADUser cmdlet?
What is the correct syntax to export the results to a CSV file using PowerShell?
Workaround
$output = @()
foreach( $user in $users )
{ $output += [pscustomobject] @{ "firstname" = $user.firstname; "lastname" = $user.lastname "pcname" = "..." }
}
# display the result
$output | format-tablewhich now gives this output:
firstname lastname pcname
--------- -------- ------
vic reeves ...
bob mortimer ...$Name = Read-Host -Prompt " "
# might contain multiple accounts
$UserAccounts = Get-ADUser -Filter "Name -like '*$Name*'"
# loop over each account adding it to the $output array
$output = @()
foreach( $UserAccount in $UserAccounts )
{ if($UserAccount.SamAccountName -like "companyname-initials*") { $UserCpu = Get-ADComputer -Filter "Description -like 'companyname-initials*'" } else { $UserCpu = Get-ADComputer -Filter "Description -like '*$Name*'" } $Output += [pscustomobject]@{ Email = $UserAccount.UserPrincipalName Login = $UserAccount.SamAccountName PCName = $UserCpu.Name }
}
# show the results
$Output | Format-Table

