How do I apply uppercase in a piped select statement in PowerShell? - powershell

I'm writing a simple PowerShell script that fetches certain fields from Active Directory using the "Get-ADUser" cmdlet and outputs them to a CSV file, which works just fine:
Get-ADUser -filter * -properties Division,Department,EmployeeID,GivenName,Sn,C,Title,Mail,ExtensionAttribute3,ExtensionAttribute4,SamAccountName -server myADserver.domain.com:3268 | select Division,Department,EmployeeID,GivenName,Sn,C,Title,Mail,ExtensionAttribute3,ExtensionAttribute4,SamAccountName | Export-Csv -notype C:\Security-ActiveDirectory.csv
I'm looking to convert the "SamAccouontName" field to all uppercase when it is output to the CSV file. I tried applying the ToUpper() and Upper(string) methods, but they give me syntax errors:
Get-ADUser -filter * -properties Division,Department,EmployeeID,GivenName,Sn,C,Title,Mail,ExtensionAttribute3,ExtensionAttribute4,SamAccountName -server myADserver.domain.com:3268 | select Division,Department,EmployeeID,GivenName,Sn,C,Title,Mail,ExtensionAttribute3,ExtensionAttribute4,ToUpper().SamAccountName | Export-Csv -notype C:\Security-ActiveDirectory.csv
Get-ADUser -filter * -properties Division,Department,EmployeeID,GivenName,Sn,C,Title,Mail,ExtensionAttribute3,ExtensionAttribute4,SamAccountName -server myADserver.domain.com:3268 | select Division,Department,EmployeeID,GivenName,Sn,C,Title,Mail,ExtensionAttribute3,ExtensionAttribute4,Upper(SamAccountName) | Export-Csv -notype C:\Security-ActiveDirectory.csv
Can anyone help me on how I can make that field uppercase and still export to the CSV file?
Thanks!
Brian

You can do it like this:
Get-ADUser -filter * -properties Division,Department,EmployeeID,GivenName,Sn,C,Title,Mail,ExtensionAttribute3,ExtensionAttribute4,SamAccountName -server myADserver.domain.com:3268 |
select Division,Department,EmployeeID,GivenName,Sn,C,Title,Mail,ExtensionAttribute3,ExtensionAttribute4,#{Label = 'SamAccountName' ; Expression = {$_.SamAccountName.ToUpper()}} |
Export-Csv -notype C:\Security-ActiveDirectory.csv

Related

Trying to export from a multivalued attribute "Proxyaddresses" to csv

Trying to export from a multivalued attribute "Proxyaddresses" from multiple OUs to csv. i am getting "Microsoft.ActiveDirectory.Management.ADPropertyValueCollection"
Here is my code,
Thanks
"ou=no gpo,ou=Staff, ou=offices,dc=ddddd,dc=ca",
"ou=Staff win10,dc=ddddd,dc=ca" | ForEach-Object {
Get-ADUser -Filter * -SearchBase $_ -Properties *
}| select name, #{Name=’proxyAddresses’;Expression={[string]::join(“;”, ($_.proxyAddresses))}} | Export-Csv c:\temp\all_proxyaddresses.csv -NoTypeInformation
Try this:
$a= "ou=no gpo,ou=Staff, ou=offices,dc=ddddd,dc=ca", "ou=Staff win10,dc=ddddd,dc=ca"
$a| ForEach-Object { Get-ADUser -Filter * -SearchBase $_ -Properties *}| select name, #{Name=’proxyAddresses’;Expression={$_.proxyAddresses -join ';'}} | Export-Csv c:\temp\all_proxyaddresses.csv -NoTypeInformation

collect samaccount powershell

I have a list of users in a CSV, but I need to collect the SamAccount attribute from each user by name in the ad.
CSV model
Script
Get-ADObject -Filter 'ObjectClass -eq "user" -and userAccountControl -eq "512"' -Properties * | Select-Object SamAccountName,CN,DisplayName, | Export-CSV -Path C:\Temp\UserAccounts.csv -Encoding UTF8 -NoTypeInformation
I'm a little lost I don't know how to do a foreach using name
I am trying but without success.
Trying to get samaccountname based on Name on csv file.
Import-Csv -Path C:\Temp\userteste.csv | foreach-Object {Get-ADUser -Filter {Name -like $_.name} -Properties Name | Select-Object samAccountName}
and export to csv file.
Why use Get-ADObject and not Get-ADUser for this? The latter gives you more of the desired properties you need in the CSV.
As aside, it is wasteful to do -Properties * if all you want is a small set of user attributes.
Something like this should work:
Get-ADUser -Filter "Enabled -eq $true" -Properties DisplayName, CN |
Select-Object SamAccountName, CN, DisplayName |
Export-Csv -Path C:\Temp\UserAccounts.csv -Encoding UTF8 -NoTypeInformation
As per your comment you need to get some extra attributes of the users listed in the CSV, you can do this:
Import-Csv -Path C:\Temp\userteste.csv | ForEach-Object {
Get-ADUser -Filter "Name -like '$($_.Name)'" -Properties DisplayName, CN |
Select-Object SamAccountName, CN, DisplayName
} | Export-Csv -Path C:\Temp\UserAccounts.csv -Encoding UTF8 -NoTypeInformation
Hope that helps

Get-ADuser attribute 'businesscategory'

I have been trying to export a list that oulines the attribute 'businesscategory'.
Here is the command:
ipmo activedirectory
get-aduser -Filter * -Properties * | select userprincipalname, businesscategory |export-csv -Path C:\Temp\businesscategory.csv -Delimiter ";" -NoTypeInformation
The thing is that I get this output under the row 'businesscategory' in my .csv :
"Microsoft.ActiveDirectory.Management.ADPropertyValueCollection"
The output is good in the Powershell console though.
I have looked for answers around the Internet but unsuccessfully so far...
Thank you guys.
It's a multivalued property so you have to create a new property and concatenate the values or select one of them
get-aduser -filter * -Properties * | select userprincipalname, #{n="businesscategory";e={$_.businesscategory -join " "}}
then it will be exported as a string in your csv.

Export to CSV only returning string length

I'm trying to get this script to export to a CSV file, it only lists the string length and not the emails i am trying to pull.
Get-ADGroup -filter {name -like 'Security Group'} |
Get-ADGroupMember -Recursive |
Get-ADUser -Properties Mail |
select -ExpandProperty Mail |
Export-Csv -NoType MyCSVfile1.csv
Export-Csv expects to receive an object, you've given it a string so it's giving you the properties of that string in the output file (that is, Length).
Drop -ExpandProperty and it will be fine.
Get-ADGroup -filter {name -like 'Security Group'} |
Get-ADGroupMember -Recursive |
Get-ADUser -Properties Mail |
Select Mail |
Export-Csv -NoType MyCSVfile1.csv

How to format multiple entries in Powershell

I'm using the following code to get a list of direct reports in Powershell:
import-module activedirectory
$identity = read-host "Enter username:"
Get-ADUser $identity -Properties directReports | select-object {$_.directReports} | export-csv "DirectReports-$identity.csv"
This outputs all of the direct reports to one row, such as "CN=User1,CN=User2,CN=User3".....
How can I modify this so it outputs each user to its own row?
I'm sure this isn't the cleanest solution:
Get-ADUser $identity -Properties directReports | select-object -expandproperty directReports|foreach-object{$_.split(",")[0].replace("CN=","")}| export-csv "DirectReports-$identity.csv"
You'll get each person's name (only) on its own line in the CSV file.
Get-ADUser $identity -Properties directReports |
Select-Object Name,#{n='DirectReports';e={$_.directReports.Split(',CN=',[System.StringSplitOptions]::RemoveEmptyEntries) -join ';'}} |
Export-Csv "DirectReports-$identity.csv"