PowerShell to delete AD Object based on timestamp? - powershell

How can I delete all the ADObject (AD User / AD Computer object) in a specific OU and below based on the specific Modified time?
Starting OU location Canonical Name:
Domain.com/OffBoarded Users
Domain.com/Old Computers
Using this command: https://learn.microsoft.com/en-us/powershell/module/activedirectory/remove-adobject

Start by getting the AD objects you want to remove, then pipe it to the remove command.
Get-ADObject command: https://learn.microsoft.com/en-us/powershell/module/activedirectory/get-adobject?view=windowsserver2022-ps
Look at example 2's "Search Base" and example 3's "specified attribute"
Sample:
$date = (Get-Date).AddDays(-30)
Get-ADObject -SearchBase 'CN=Offboarded Users,DC=Domain,DC=Com' -searchScope 2 -filter "whenChanged -le $date" | Remove-ADObject
Get-ADObject -SearchBase 'CN=Old Computers,DC=Domain,DC=Com' -searchScope 2 -filter "whenChanged -le $date" | Remove-ADObject

Related

Enable prevention of accidental deletions of DNS zones stored in Active Directory Domain Services (ADDS)

I want prevent accidental deletion for my dns zones on my domain. I have try to perform this action via powershell but i have some doubts.
I have used the following command, i found this on Microsoft:
Get-ADobject -Server "<DomainController_fqdn>" -Filter {objectclass -eq "DNSZone"} -SearchBase "DC=DomainDNSZones,<DomainDN>" | Set-ADObject -ProtectedFromAccidentalDeletion $true
Bellow is this command adapted to my domain:
Get-ADobject -Server "myservername+fqdn" -Filter {objectclass -eq "mydominzone"} -SearchBase "DC=DomainDNSZones,DC=aa,DC=bb,DC=cc" | Set-ADObject -ProtectedFromAccidentalDeletion $true
The script seems to run ok , but I don't receive any message that that fag was changed on "true".
How can i check if this setting was applied or not?
You just have to tell Get-ADObject to show that property with the -Properties parameter:
Get-ADobject -Server "myservername+fqdn" `
-Filter {objectclass -eq "mydominzone"} `
-SearchBase "DC=DomainDNSZones,DC=aa,DC=bb,DC=cc" `
-Properties ProtectedFromAccidentalDeletion
Interestingly, there isn't actually any flag in Active Directory for that. All it does is add a "deny delete" permission for "Everyone" to the account. But both Set-ADObject and Get-ADObject translate that into a property that you can set and read. And even AD Users and Computers shows it as a checkbox.

how to remove all OU from AD via powershell

I am testing powershell with active directory,
have created list of OU in particular domain, but unable to remove all the OU from AD, Want to remove all OU except built in from AD
below is the script i am using, but it is giving access denied-
using administrator id
Get-ADOrganizationalUnit -Filter {Name -notlike "Domain Controllers"} -SearchBase (Get-ADDomain).Distinguishedname -SearchScope OneLevel | Remove-ADOrganizationalUnit -Recursive -Confirm:$false
OU's are protected by default, you have to remove that flag before deleting:
Set-ADObject -ProtectedFromAccidentalDeletion $false

Trying to change displayname in AD LDS with Powershell

I have an online learning management system with most of its data in sql server but its page structure and contents in an AD LDS instance. I have a few classes which have already been set up with 50 pages called "Unused Portfolio 01" through "Unused Portfolio 50". These are their displaynames. Their CNs are "Portfolio 01" through "Portfolio 50".
I need to change the displaynames to each have a different student's name, in the format "Last, First". I am trying to use Active Directory Module for Windows Powershell. Right now I am on a test server trying to make this work for just a few pages. I can't manage to change more than one displayname at a time.
I can get a list of the objects for these pages:
Get-ADObject -Server localhost:389 -filter 'displayname -like "*Portfolio*" -and cn -like "Portfolio*"' -searchbase 'CN=DMIN2013-LMS 101-02,CN=LMS 101,CN=LMS,CN=Academics,CN=Portal,O=Jenzabar,C=US'
I get the distinguishedname, name, objectclass, and objectguid for all three expected objects and no unexpected objects. Great.
I can change any one object's displayname at a time:
set-adobject -Server localhost:389 -identity "CN=Portfolio 01,CN=DMIN2013-LMS 101-02,CN=LMS 101,CN=LMS,CN=Academics,CN=Portal,O=Jenzabar,C=US" -displayname "testing"
The specified object has its displayname changed to "testing". Awesome.
I'm trying to use this to change all of the displaynames for these three objects to "testing" at once, and obviously I have something wrong because it is not working:
Get-ADObject -Server localhost:389 -filter 'displayname -like "*Portfolio*" -and cn -like "Portfolio*"' -searchbase 'CN=DMIN2013-LMS 101-02,CN=LMS 101,CN=LMS,CN=Academics,CN=Portal,O=Jenzabar,C=US' | foreach-object 'set-adobject -Server localhost:389 -identity $_ -displayname "testing"'
The ultimate goal is that I will have a csv file (which I will have gotten from an sql query from the sql server) containing a "number" column 01 to 50, a "lastname" column, and a "firstname" column, and I will change each page's display name to match ", " for each student, but I'm not at that point yet.
Thanks for any help you can offer.
Foreach-Object uses a script block and not a string, so use:
something | Foreach-Object {Do something with $_}
This might be due to the fact that $_ contains an object and not its DN. $_.DistinguishedName. Also what ojk says
Get-ADObject -Server localhost:389 -filter 'displayname -like "*Portfolio*" -and cn -like "Portfolio*"' -searchbase 'CN=DMIN2013-LMS 101-02,CN=LMS 101,CN=LMS,CN=Academics,CN=Portal,O=Jenzabar,C=US' | foreach-object {set-adobject -Server localhost:389 -identity $_.DistinguishedName -displayname "testing"}

get all computer accounts and remove-ADPrincipalGroupMembership

I'm trying to remove all the principal group memberships starting with the name of all computer accounts in one specific ou.
I've tried browsing to the OU with the AD provider, typing gci and getting a list of all the computers in the ou to find their ADPrincipalGroupMembership which works. Also, using get-adcomputer -searchbase <ou> -filter * works too. But I can't then remove every group that each machine is a member of.
When I then try to expand on that with remove-ADPrincipalGroupMembership, my input for the groups to remove are system.string and remove-ADPrincipalGroupMembership won't accept that. I have something like this so far/
Get-ADComputer -SearchBase 'OU=blahblah' -Filter * |
Remove-ADPrincipalGroupMembership -MemberOf (Get-ADGroup -Filter 'name -like "17"')
I've read help and examples but I can't find how to do this. I don't want to give up and just use the gui :)
thank you
You can try this...I am not able to test it to confirm it works, but I think it should.
$Comps = Get-ADComputer -SearchBase 'OU=blahblah' -Filter * -Prop MemberOf
Foreach ($Comp in $Comps)
{
$Groups = $Comp.MemberOf | ? {$_ -like "CN=17*"}
if ($Groups)
{
Remove-ADPrincipalGroupMembership -Identity $Comp -MemberOf $Groups -Whatif #-Confirm $False
}
}
Assuming it works with the -whatif statement, by default I believe that command will prompt you if you're sure about each removal which could be a pain so you could uncomment -confirm $false to try and avoid that.
Also it is assuming the distinguished name of each group is going to be something along the lines of
CN=17groupA,OU=Computer Groups,OU=Computer,DC=TEST,DC=NET

Loop through multiple Active Directory Ou's in Powershell

How do you search multiple OU's in Active Directory. Say if there are 4 OU's for different users, and need to search only 3 of the 4.
Currently I am using the below to search one path, how would I expand that to search multiple OU's.
$OU='AD Path'
Get-ADUser -SearchBase $OU -Properties Lastlogondate -filter {lastlogondate -lt $DisableDays}
It looks like -searchbase takes <string>, so you would need to loop through OUs.
The following query would get users in each OU:
$OU=#('cn=users,dc=xyz,dc=com','ou=companyusers,dc=xyz,dc=com')
$ou | foreach { get-aduser -searchbase $_ ...}