My requirement is pretty simple but I am not that much great with scripting so posting my script here to check if that's correct.
Import-Module ActiveDirectory
#Using Different account to connect to AD
$Username = ""
$Password = ""
$ADCred = New-Object System.Management.Automation.PSCredentials("$Username","$Password")
#Starting Powershell with above credentials
Start-Process powershell.exe -Credential $ADcred
#ServerName
$HostName = $env:COMPUTERNAME
#Create a new Security group in active directory
New-ADGroup "GroupName_$Hostname" -GroupScope Global -GroupCategory Security -Path "OU=Security Groups,OU=Infrastructure Management,OU=Special Objects,DC=xyz,DC=mycompany,DC=com"
#Add New Member to the AD
Add-ADGroupMember "GroupName" -Members "MyServer-1"
Will this script works for me or is there any better way we can write this script. Please let me know.
Devraj88