Specify a domain controller using get-aduser in powershell
Get-ADUser -identity $ntaccount1 -properties name, samaccountname, mail, enabled, passwordlastset
Is it possible when viewing user account information in powershell to specify the domain controller to use? We have DCs that receive data faster than others.
From Get-Help Get-ADUser -Parameter *
-Server <string>
Specifies the Active Directory Domain Services instance to connect to, by providing one of the following values for a
corresponding domain name or directory server. The service may be any of the following: Active Directory Lightweight Domain
Services, Active Directory Domain Services or Active Directory Snapshot instance.
Domain name values:
Fully qualified domain name
Examples: corp.contoso.com
NetBIOS name
Example: CORP
Directory server values:
Fully qualified directory server name
Example: corp-DC12.corp.contoso.com
NetBIOS name
Example: corp-DC12
Fully qualified directory server name and port
Example: corp-DC12.corp.contoso.com:3268
The default value for the Server parameter is determined by one of the following methods in the order that they are listed:
-By using Server value from objects passed through the pipeline.
-By using the server information associated with the Active Directory PowerShell provider drive, when running under that drive.
-By using the domain of the computer running Powershell.
The following example shows how to specify a full qualified domain name as the parameter value.
-Server "corp.contoso.com"
Required? false
Position? named
Default value
Accept pipeline input? false
Accept wildcard characters? false
I know this is a little old question, but I would like to expand on this answer to help anyone with a similar request.
Below is a description of a specific domain controller that the whole script can use ... Why can you do this when the -server option is available for Get-ADUser, New -ADUser, Set-ADObject, etc.
Ok, I put together a script that creates an AD user, sets a few properties, and creates an exchange mailbox. However, one set of properties revolves around RDS properties in a 2008 R2 user account that cannot be set from within New-ADUser. I had to create a function that calls ADSI and uses psbase.invokeSet to update the settings. There are no parameters for the server-parameter I know of.
That won't make much of a difference in itself, but the script also creates an Exchange mailbox for the user. Since my Exchange server is in a different AD site from my workstation, a user account is created on my local DC, but the mailbox is not installed because the DC in the same site as the Exchange server has not yet received a replicated copy of the new account user.
The solution I found is as follows and is kindly provided by http://www.joseph-streeter.com/?p=799
By loading import-module activedirectory , you will have access to AD settings in the New-PSDrive cmdlet , which, among other things, allows you to define a new Active Directory Provider to work with.
New-PSDrive -Name <<NameofYourChoice>> -PSProvider ActiveDirectory -Server <<DC Server>> -Root "//RootDSE/" -Scope Global
Once created, you can change the running Provider with the following command.
CD <<NameofYourChoice>>:
To view an existing list of suppliers, enter Get-PSDrive . AD is the default Active Directory Provider created by using the ActiveDirectory cmdlet. You should also see your newly created Provider.
So, for example, if my remote DC is called RemoteDC, I would run:
New-PSDrive -Name RemoteAD -PSProvider ActiveDirectory -Server RemoteDC -Root "//RootDSE/" -Scope Global
to create a new provider called RemoteAD. If I then run:
CD RemoteAD:
Any additional active commands associated with a directory in a script or active shell will work with the new RemoteAD provider. If I need to go back to the original ISP, I'll just type
CD AD:
Hope someone finds this useful ...