connecting to AD with the credentials and creating AD Security group RRS feed

  • Question

  • 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  

    $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.


    Tuesday, August 28, 2018 1:17 PM


  • As written the script will not work.  Starting PowerShell as another user will only create a new window and the code will not be executed in that window/session.


    Tuesday, August 28, 2018 1:32 PM