Ralluhb
Ralluhb

Reputation: 225

List of users to match a list of job titles in AD

I have a list of Job titles in a csv file and need to match these to whichever user has the title in AD.

CSV Contents is like this:

JobTitle1
JobTitle2
JobTitle3

The following command will work to get a list of all users with the JobTitle1 title:

Get-ADUser -Filter "Title -like 'JobTitle1'"

However I can't get this to work with the CSV file as an array, it doesnt return any output:

$Titles = Get-Content -Path "c:\jobtitles.csv"  
ForEach ($Title In $Titles)
{
    Get-ADUser -Filter {Title -like '$Title'} 
}

Upvotes: 2

Views: 2622

Answers (1)

Martin Brandl
Martin Brandl

Reputation: 59021

In PowerShell a variable within single quotes doesn't get populated so you have to use double quotes instead - or just omit them:

$Titles = Get-Content -Path "c:\jobtitles.csv"  
ForEach ($Title In $Titles)
{
    Get-ADUser -Filter {Title -like $Title} 
}

Upvotes: 2

Related Questions