Active Directory (AD)
13 TopicsAdding AD users to a specific security group
Hi Everyone, Sorry if this question has already been asked as I couldn't find an answer. I’m trying to write a PowerShell script that runs as a scheduled task to add AD users to a specific AD security group. The goal is for this to run daily. The script will first check the users' OU to determine if they are already members of the security group. If they are, it will skip them; if they are not members, it will add them to the group. I have created the following script, but I’m unsure if it's the best approach. Additionally, can this script be executed on a server that doesn’t have Active Directory installed? If AD must be installed, would it be ideal to run it on a Domain Controller? # Check if Active Directory module is already imported, import only if necessary if (-not (Get-Module -Name ActiveDirectory)) { Import-Module ActiveDirectory } # Define the base OU and security group $BaseOU = "OU=W11_USERS,DC=W11,DC=NET" $SecurityGroup = "HR" # Get all users from W11_USERS and its sub-OUs $Users = Get-ADUser -SearchBase $BaseOU -SearchScope Subtree -Filter * # Loop through each user and check group membership before adding foreach ($User in $Users) { $UserDN = $User.DistinguishedName # Check if user is already a member of HR $IsMember = Get-ADGroupMember -Identity $SecurityGroup | Where-Object { $_.DistinguishedName -eq $UserDN } if (-not $IsMember) { Try { Add-ADGroupMember -Identity $SecurityGroup -Members $User -ErrorAction Stop Write-Host "Added $($User.SamAccountName) to $SecurityGroup" -ForegroundColor Green } Catch { Write-Host "Failed to add $($User.SamAccountName): $_" -ForegroundColor Red } } else { Write-Host "$($User.SamAccountName) is already a member of $SecurityGroup" -ForegroundColor Yellow } } Write-Host "User addition process completed."132Views0likes1CommentQuery OU - Get Groups - Get Group Members - Export to CSV
The AI result did not work I am getting an error. The error is "Get-ADGroupMember: The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input." Import-Module ActiveDirectory # Specify the OU where your groups are located $SearchBase = "CN=test,DC=test,DC=LOC" # Get all groups within the specified OU $Groups = Get-ADGroup -Filter * -Properties * -SearchBase $SearchBase # Initialize an empty array to store group members $GroupMembers = @() foreach ($Group in $Groups) { $Members = $Group | Get-ADGroupMember foreach ($Member in $Members) { $Info = New-Object psObject $Info | Add-Member -MemberType NoteProperty -Name "GroupName" -Value $Group.Name $Info | Add-Member -MemberType NoteProperty -Name "Description" -Value $Group.Description $Info | Add-Member -MemberType NoteProperty -Name "Member" -Value $Member.Name $GroupMembers += $Info } } # Export the results to a CSV file $GroupMembers | Sort-Object GroupName | Export-CSV C:\temp\group_members.csv -NoTypeInformationSolved337Views0likes5CommentsRepeating a task
Hi all, We are currently using Power Automate to create on-prem users. The problem we've hit is that the UPN suffix is blank (different issue). To get around this, I've made a PowerShell script that runs every 5 mins on a scheduled task. As I've found this evening, if there is more than one user that is missing a UPN suffix, it fails. $LocalUsers = Get-ADUser -Filter {UserPrincipalName -notlike '*@*'} -Properties UserPrincipalName -ResultSetSize $null $UPN = $LocalUsers.UserPrincipalName + "@tww-lab.co.uk" Set-ADUser -Identity $LocalUsers.SamAccountName -UserPrincipalName $UPN We want the script to find an entry missing a UPN suffix and correct it to Firstname.Lastname @ UPN Is there a way we can do this or is there a better way to do this?Solved486Views0likes2CommentsHow to fetch / filter users from AD faster using Get-ADUser command.
Recently I saw few scripts which are fetching users from AD like below mentioned. Get-ADUser -LDAPFilter "(whenCreated>=$date)" or Get-ADUser -filter {Enabled -eq $True -and PasswordNeverExpires -eq $False -and PasswordLastSet -gt 0} or Get-ADUser -Filter 'Enabled -eq $True' But using like above is taking quite a lot of time or sometimes giving Timeout error. is there any way can make this faster? Will using -LDAPFilter instead of -Filter make it faster? Error Message: The operation returned because the timeout limit was exceeded.Solved4.8KViews0likes2CommentsExport AD manager name
Hi all, I'm not advanced with PowerShell and won't admit to being, but wondered if someone may be able to help me with this one ? This is to work with AD on-prem, not AAD I've got the below script which works perfectly at the moment, but i'm looking to take this a step further, which is that instead of displaying the Manager along with the OU detail, i'd like this to display only the Manager display name, whilst also exporting the other information as in the script as required. Any help would be greatly appreciated. Get-ADGroup "UK-Admin-Users" -Properties Member | Select-Object -ExpandProperty Member | Get-ADUser -Properties * | select name, enabled, samAccountName, CanonicalName, manager | Export-CSV C:\Temp\PS\Output\Usersdetail.csvSolved2.3KViews0likes1CommentSite to Zone Assignment List - Powershell
I need to replicate the steps of adding a list of URLs to the Site to Zone Assignment List of a GPO. Is there a way to edit that GPO via PowerShell, enable Site to Zone Assignment List, and pass the list of URLs to it? - Open the Group Policy Management Editor. Go to User Configuration > Policies > Administrative Templates > Windows Components > Internet Explorer > Internet Control Panel > Security Page. Select the Site to Zone Assignment List. Select Enabled and click Show to edit the list. The zone values are as follows: 1 — intranet, 2 — trusted sites, 3 — internet zone, 4 — restricted sites. Click OK. Click Apply and OK.Solved16KViews0likes1CommentGet users from CSV, foreach user, get country, name, etc., then Export
Hi! I have a problem with PS query. I have a CSV file with +1000 userprincipalname. Id like to export that UPN and for each user get info like userprincipalname, title, country, department, enabled, then export to excel. My example queries: $csv = gc "C:\New folder\Users.csv" $Users=@() $csv | Foreach{ $elements=$_ -split(";") $Users+= ,@($elements[0]) } ForEach ($User in $Users) { Get-ADUser -filter "userPrincipalName -eq '$User'" -Properties * | Select-Object userprincipalname, title, country, department, enabled } OR $Users = Get-contect -path "C:\New folder\Users.csv" ForEach ($User in $Users) { Get-ADUser -filter "userPrincipalName -eq '$User'" -Properties * | Select-Object userprincipalname, title, country, department, enabled } OR $Users = Get-contect -path "C:\New folder\Users.csv" ForEach ($User in $Users) { Get-ADUser -filter $User -Properties * | Select-Object userprincipalname, title, country, department, enabled } My excel has UPN only, like: email address removed for privacy reasons email address removed for privacy reasons etc. No headers. Someone will help? 😞7.4KViews0likes2CommentsHelp with PS script for computers in AD onprem
Hi guys, I want to automate a task by adding new computers to the domain. By default, they appear in Computers OU and we have different OUs per office and computers. My idea is a script to run every 20 min as scheduled tasks which will check for new computers in the computers OU and then move computers to specific OU based on the name of the PC. For example, when a computer name begins with BN1-N- then it will be moved to Notebooks OU in BN1 OU, when computer name begins with KN1-W- then it will be moved to Workstations OU in KN1 OU and so on. All solutions were related to Identity with specific computer name. Is it possible to be done? Thanks631Views0likes2CommentsAssigning groups to AD user.
Hello everyone, I have the following situation: In my work we are always hiring people and depending on their position they may be assigned to different groups. So the process of creating users and assigning them to groups has become a nightmare because my team always gets it wrong, so I either have to add or remove a group. So it occurred to me to create a kind of template to make sure that when they create a user for a specific area they put the groups that corresponds, however still have been wrong, so it occurred to me that it might be possible to create that template in files and through a PowerShell script assign the groups to the user, is that possible? Thank you in advance,1.1KViews0likes2CommentsGetting users and group
Hello everyone, Hopefully you are find when you read this. I have pull out all my users with their groups in a CVS file, so I'm aware to approach this the command Get-ADGroupMember is the one for that but even I have read some articles about it, I haven't be able to get the info. Do you mind give me a hand with that and explaining how to do it? Thank you in advance,Solved1.7KViews0likes7Comments