Add a user to a domain group and set the user privileges to certain folder - cmd

I have a user in my workplace domain, I want to add him to a specific domain group then assign him some privileges on a specific folder.
I wonder how this can be done using command line or a more automated process than doing it step by step as I do this quite often.
I'm using AD on Windows 10
Looks like dsmod group can be used but I don't know how.
If I have a user with username userh01 in domain mydom how I can add him automatically to group mydomgroup1?
I've tried this command:
dsmod group "mydomgroup1" -addmbr "userh01"
but I get this error
dsmod failed:Value for 'Target object for this command' has incorrect format.
Any advice?

Maybe using powershell to add memeber to a domain group is an alternative way.
here below th script for example
Add-ADGroupMember -Identity "Groupmane" -Memebers "Username to add"
Add-ADGroupMember -Identity "mymdomgroup1" -Memebers "userh01"
ps:you may need to import active diretory modul. before using Add-ADGroupMeber parameter use this command 'Import-Module ActiveDirectory' at begining
for different syntax and detailed description to add-adgroupmember parameter follow this link
https://learn.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2008-R2-and-2008/ee617210(v=technet.10)

Related

Windows Audit Policy/Registry Key Command Check To Only Apply On Domain Controllers

I am trying to craft a command that would run against all of my Windows machines to check if the "Audit Distribution Group Management" audit policy setting is set to "Success and Failure". I would only like to apply this check to Domain Controller servers and for any other server type to echo out something like "NoCheckRequired", is this possible?
I tried to create an if-else statement on PowerShell for this, but it was not successful.
I tried to use the "wmic.exe ComputerSystem get DomainRole" command to find out the type of machine, values 4 / 5 mean DC server from my understanding, and using an IF statement, I tried to match those values and check if the group policy audit settings were set and for any other values returned other than 4 / 5
wmic.exe ComputerSystem get DomainRole outputs the property name on a separate line before outputting the actual value, so comparing to the number 4 (as an example) will not work.
Instead, use the Get-CimInstance cmdlet:
$CS = Get-CimInstance Win32_ComputerSystem
if($CS.DomainRole -in 4,5){
# We're on a Domain Controller
}
elseif($CS.DomainRole -in 1,3) {
# We're on a Domain member
}
else {
# We're on a workgroup machine
}
Get-ADComputer -Filter 'primarygroupid -eq "516"'
Will filter the Domain controller

Powershell: Add "Domain Admins" Group to ALL Fileshares on Server

I am currently trying to add a group called "Domain Admins" to all fileshares, we have over 300. I would like to automate this so I don't have to do this manually for each one, and would then like to remove the local administrator group from each fileshare as well.
I currently am able to get all fileshares names and permissions of each one with:
$shares = Get-SmbShare
foreach($share in $shares) {
$share
(get-smbshare $share.Path)| select #{Label="Path"; Expression = { Convert-Path $_.Path }} -ExpandProperty Access
}
But I can't seem to find any resources that demonstrate how to add a group to all fileshares. I know how to add a group to a specific file share, but not all. If anyone can share some tips, that would be great!

Server 2012 Powershell Check Members of a Group

I have users and groups in the Users folder of a Windows 2012 Server. I want to check if a user is a member of a group and if not, add to the group. To list the members of a group, I've tried everything including:
get-adgroupmember -identity "cs99group"
which produces the error
get-adgroupmember : Cannot find an object with identity: 'cs99group' under: ...
The following works perfectly
get-adgroupmember -identity "Administrators"
Of course the Administrators group is in the Builtin folder and cs99group is in the Users folder. What am I doing wrong?
Can you try with the parameter -recursive added?
Does the command Get-AdGroup work?
If so, try Get-AdGroup "cs99group" | get-adgroupmember

Removing AD Group Membership (PowerShell)

I have a script for people who leave my organization. It basically creates a user backup folder and file before changing the password, removing the manager, backing up all member of groups, removing all group memberships, disabling the account and moving to different OU.
The part that i seem to be stuck on is the membership removals. This is what i have for that task:
Get-ADPrincipalGroupMembership -Identity "$Username" | % {Remove-ADPrincipalGroupMembership -Identity "$Username" -MemberOf $_}
This DOES work but I am prompted to confirm Yes or No to each group. When i add:
-confirm $false
at the end, none of the groups are removed. How can I get it to not ask for confirmation while still removing the groups?
Thank You!
This worked for me:
Get-ADPrincipalGroupMembership -Identity "$Username" | % {Remove-ADPrincipalGroupMembership -Identity "$Username" -MemberOf $_ -Confirm:$False}
No prompts and all of the removals worked just fine.
I wonder if there is a way to suppress the error message thrown up with respect to the user's Primary Group -- DomainMembers, in our case. I'm perfectly happy with the result (the command won't remove the user from his primary group, while the user is removed from all others), but the error message just looks ugly.`

How do I export Active Directory membership?

I am in an organization with thousands of users and some of them are not in the correct Active Directory group. I can access most of the groups through dsquery Rundll dsquery.dll OpenQueryWindow, but the way the groups are configured, it takes forever to check. Microsoft doesn't provide any information I've seen to export this to a text file or other source, but there must be a better way to get this information.
I assume you mean organizational unit (OU) instead of group.
Use PowerShell. You do need RSAT installed. Then you can run:
Import-Module ActiveDirectory
Get-ADUser -Filter * -SearchBase "ou=testou,dc=iammred,dc=net"
Change the OU to the OU you want to look at.
Or if you really did mean group:
Get-ADGroupMember -identity "GROUPNAME" -Recursive

Resources