I am currently attempting to write a powershell script that will update the Job Description of Various Users in Active Directory via a CSV File
The identifier I have been given to use is the User's Email Address instead of Username (Which I think would of been easier!)
Can anyone assist as I am struggling to write anything effective that works! :(
Ok, suppose your .csv looks like this:
"email","jobtitle"
"user1#mydomain.com","New job description for user1"
"user2#mydomain.com","New job description for user2"
"user3#mydomain.com","New job description for user3"
you could then do something like
Import-Module ActiveDirectory
Import-CSV -Path <PATH-TO-YOUR-CSV-FILE> | Foreach-Object {
# properties from the csv
$mail = $_.email
$title = $_.jobtitle
Get-ADUser -Filter {(mail -eq "$mail")} | Set-ADUser -Title $title
}
Related
I was wondering if you could help me.
I was hoping you could assist me in creating a script where I would specify a list of usernames (could be either in .txt or csv file) to be deleted from AD in PowerShell.
I know there is command to do this but I have to change the username every time I run the command:
Remove-ADUser Username
Thanks for your assistance guys.
UPDATE 1
Can you stop changing the question (I would prefer help instead!)
I am looking into this myself and will attempt to answer the question (I am still learning so be patient!).
I have come up with a script:
$users=Get-Content -Path C:\Users\Me\Desktop\disableusers.txt
ForEach ($user in $users)
{
Remove-ADUser -identity $user
}
I know its not the most slickest of scripts but if it does the job I am happy.
I have also found something like this:
$Users = Import-Csv 'c:\temp\yourcsv.csv'
Foreach ($User in $Users)
{
Try
{
# Verify that users from your CSV exist in Active Directory
Get-ADUser $User -ErrorAction Stop | Out-Null
Remove-ADUser $User -Confirm:$False
}
Catch
{
Write-Host "Username '$User' not found in Active Directory"
}
}
But the above script must take this into consideration:
Depending how your csv looks like you might need to change $User to $User.SamAccountName or whatever that column is named in your csv.
UPDATE 2
I tried to do the CSV method but I get error stating it can't find the usernames.
I tried with CSV only containing usernames and altering the script and also with the field header of SamAccountName, I know this will sound stupid but once I have a field header of SamAccountName does that mean all $Users and now $User.SamAccountName in the CSV script?
I'm trying to find a PowerShell script that updates the title attrubute in AD for a large number of users. I was hoping to find a script that imports the changes from a csv file and updates the atribute only for the users in the list. I found the below script but apparently it is working only for Azure AD, and I need it for the local AD. Perhaps someone more switche on than me can help me amend the below script.
#Import Active Directory module
Import-Module ActiveDirectory
#Import CSV File to set variable for the user’s logon name + update data + delimiter
$Users = Import-CSV -Delimiter ";" -Path "c:\psscripts\users.csv"
#Using your code to filter AD Sam Accounts listed CSVData is listed with the information you wish to update
Foreach($user in $users){
#Using your code to filter AD Sam Accounts Based on column samaccountname in the csv file
Get-ADUser -Filter "SamAccountName -eq '$($user.samaccountname)'" | Set-ADUSer `
-title $($User.Title)`
}
That code is fine, beyond some variable consistency and lack of checks, and does target local AD, though use of that deliminator would likely be unusual if you're just using a standard csv file. If you have the data in an excel document with the column headers of "SamAccountName" (typically email addresses) and "Title", and then save the file as a csv, the below amended code should work for you. Added logic to test for blank Title, as you can't assign a blank value to an attribute.
#Import Active Directory module
Import-Module ActiveDirectory
#Import CSV File with AD SAM account and Title data from users.csv in the C:\psscripts directory of your computer
$Users = Import-CSV -Path "c:\psscripts\users.csv" | Where {$_}
#Filter AD Sam Accounts listed in CSV and update title for listed accounts
Foreach($user in $Users){
#Check for value of $user.Title in case of null value
If ($user.Title){
#Filter AD Sam Accounts Based on column SamAccountName in the csv file and update the account Title field
Get-ADUser -Filter "SamAccountName -eq '$($user.SamAccountName)'" | Set-ADUSer -Title $($user.Title)
}
else {
#Filter AD Sam Accounts Based on column SamAccountName in the csv file and clear the account Title field
Get-ADUser -Filter "SamAccountName -eq '$($user.SamAccountName)'" | Set-ADUSer -clear -Title
}
}
I'd recommend testing it on a test user account or two before going whole hog on your actual list. Goes without saying that you need to be logged into a PS session as a domain account with adequate privileges to make the changes to the accounts when running the script. VS Studio Code is a good environment to work in, and you can launch the program as the elevated account (shift + right-click program icon, choose run as a different user) within your normal account environment, to sandbox the privileges to just what you're working on in VS Studio Code.
If you are trying to work in Azure AD, you'd need to add these lines and approve your account access request within Azure, depending on your tenant setup, to actually run the script successfully. Depending on the tenant configuration, this may be required in a hybrid AD/Azure AD environment regardless of your intent to apply to local AD.
Connect-MgGraph -Scopes "User.ReadWrite.All", "Directory.ReadWrite.All"
Select-MgProfile -Name "beta"
Best regards, no warranties given or implied, please accept as answer if this works for you.
I'm trying to run a command again some mailboxes, after obtaining the mailbox names from a Retention Compliance Policy.
I can export the GUID to a CSV and then call the CSV, but I'm trying to limit my use of CSV's
The Below works with a CSV file
$Accounts = Import-Csv 'C:\Temp\MailboxInfoGUID.csv'
ForEach($Account in $Accounts)
{
$Guid = $null
$Guid = #()
$Guid = $Account.ArchiveGuid
Start-ManagedFolderAssistant -Identity $Guid -FullCrawl}
$Users = Get-RetentionCompliancePolicy $LabelPolicyName -
DistributionDetail | Select - ExpandProperty ExchangeLocation | Select -
ExpandProperty Name
I'd like to run the command without using a CSV file, but to get the mailbox identity I need to look in the Compliance Policy
How can I use the mailbox names obtained from the policy to then run a crawl on the mailbox
If I understood you correct, you would like the following:
Get the mailboxes from Get-RetentionCompliancePolicy.
Iterate through each of those mailboxes grabbing the GUID property.
Using Get-Mailbox.
Pass the newly obtained GUID to Start-ManagedFolderAssistant.
Given that's what you're after, the following should work:
(Get-RetentionCompliancePolicy $LabelPolicyName -DistributionDetail).ExchangeLocation.Name |
ForEach-Object -Process {
Start-ManagedFolderAssistant -Identity (Get-Mailbox -Identity $_).ArchiveGuid -FullCrawl
}
Due keep in mind that I don't have the ExchangePowerShell module installed so I am going off just logic; you may have to adjust the properties.
forgive me for breaking any rules. I'm a brand new StackOverflow user, and a powershell noob. I'm working on a work project where I import a CSV file that contains a list of users and their respective managers. I then need to create an email that contains a table with the users for each respective manager.
I know how to import the csv file. I've gotten a basic email sending code created. I'm completely lost as to how to appropriately get the users and managers associated with each other.
For example, my csv is formatted with headers like this:
First Name Last Name Username Manager
Bob Dole BDole Jsmith
John Doe JDoe Anoob
Jane Doe JDoe1 Jsmith
etc.
Could someone please point me in the right direction? I've tried using a hashtable and an array, with no success. This is likely due to my noob status.
Please let me know if there is more information I should add. I don't have any real code written at this point since nothing has worked, so please pardon the very limited code in the post.
$users = import-csv "Path goes here"
"My code for associating the users and managers would go here"
"Email code goes here"
$users = Import-Csv .\test.csv -Delimiter ";"
$cred = Get-Credential # enter the credentials for your email account
from where you will send the mail
$users | Group-Object -Property Manager | % {
$managername = $_.name
write-host $managername
$body = $_.group | format-table | ConvertTo-Html
write-host $body
Send-MailMessage -to "$managername#some.com" -Body $body -From "yourmail" -SmtpServer "yoursmtpserver" -Port "your smtp port" -Subject "mailsubject" -Credential $cred -BodyAsHtml
}
If you dont want to enter your mail credentials every time you can save them as following.
get-credential | export-clixml -path "somepath"
later on you can read the credentials from you os:
$cred = import-clixml -path "somepath"
I am not exactly sure I understand what kind of output you want, but here is a suggestion. If you have a csv file named workers.csv, this should sort entries by the manager name:
contents of workers.csv:
first last username manager
john, doe, jdoe, boss1
jan, doe, jjdoe, boss2
jim, ddi, jddi, boss2
didi, goo, didigoo, boss1
at the Powershell prompt, type:
$managers = "boss1","boss2"
(then type enter )
then type:
foreach ($item in $managers) {
cat ./workers.csv | Select-String -Pattern "$item"
| out-file -append workers.csv
}
I am a kind of a newbie in Powershell yet, can someone please tell me how i can create a loop (purpose is to add the info to all the people (usernames) in the .CSV list)
This is what i got now, tryed many things but still no good..
Import-Module ActiveDirectory
$username = Import-Csv c:\user.csv
Set-ADuser -Identity $Username -Company "Company name"
Try a foreach loop.
Example:
foreach($user in $username)
{
//some action for example
Set-ADuser -Identity $user -Company "A Company"
}
Reference:
http://www.powershellpro.com/powershell-tutorial-introduction/logic-using-loops/
I'd also recommend adding some sort of logging (using either add-content or write-host) to track the progress of the script, if the file has a lot of entries.
regards
Arcass