Get-Help shows different outputs on different systems - powershell

When I use the following cmdlet:
Get-Help Get-ADUser -Parameter identity
On Windows 7 with RSAT installed connecting to Windows Server 2012 R2, I get the following output:
-Identity <ADUser>
Specifies an Active Directory user object by providing one of the following property values. The identifier in
parentheses is the LDAP display name for the attribute.
Distinguished Name
Example: CN=SaraDavis,CN=Europe,CN=Users,DC=corp,DC=contoso,DC=com
GUID (objectGUID)
Example: 599c3d2e-f72d-4d20-8a88-030d99495f20
Security Identifier (objectSid)
Example: S-1-5-21-3165297888-301567370-576410423-1103
SAM account name (sAMAccountName)
Example: saradavis
The cmdlet searches the default naming context or partition to find the object. If two or more objects are found
the cmdlet returns a non-terminating error.
This parameter can also get this object through the pipeline or you can set this parameter to an object instance
This example shows how to set the parameter to a distinguished name.
-Identity "CN=SaraDavis,CN=Europe,CN=Users,DC=corp,DC=contoso,DC=com"
This example shows how to set this parameter to a user object instance named "userInstance".
-Identity $userInstance
Required? true
Position? 1
Default value
Accept pipeline input? true (ByValue)
Accept wildcard characters? false
However when I use it on a Windows Server 2012 R2 or 2016 with WMF 5.1 installed I only get the following:
-Identity <ADUser>
Required? true
Position? 1
Default value
Accept pipeline input? true (ByValue)
Accept wildcard characters? false
Any idea what I'm doing wrong?

Get-ADUser command comes from a 'system module' that can be different from an OS to an other even with the same version of WMF and even if the version number shown by 'Get-Command Get-ADUser' is the same (1.0.0.0) ... so help content may be different too.
By the way, I get this result on my Windows 2012 R2 with WMF 5.1
-Identity <ADUser>
Specifies an Active Directory user object by providing one of the following property values. The identifier in
parentheses is the LDAP display name for the attribute. The acceptable values for this parameter are:
-- A Distinguished Name
-- A GUID (objectGUID)
-- A Security Identifier (objectSid)
-- A SAM Account Name (sAMAccountName)
The cmdlet searches the default naming context or partition to find the object. If two or more objects are found,
the cmdlet returns a non-terminating error.
This parameter can also get this object through the pipeline or you can set this parameter to an object instance.
Required? true
Position? 1
Default value
Accept pipeline input? True (ByValue)
Accept wildcard characters? false
You may try Update-Help to download latest PowerShell help files (if your server is connected to Internet...).

Related

Set default server in powershell for AD related commands

Recently my powershell scripts require to explicitly say which domain I want to connect to. Is it necessary to write this for each command? Or can I set it somehow once in the beginning of the script.
Instead of
Get-ADUser -Server server otherparameters
could I write in the beginning something like
Set-default server to connect to
?
Is it necessary to write this for each command?
No!
You can specify a default parameter value for a parameter belonging to one or more cmdlets by assigning it to the $PSDefaultParameterValues automatic variable:
$PSDefaultParameterValues['*-AD*:Server'] = 'mydc.mydomain.tld'
Any cmdlet you subsequently invoke that matches the *-AD* pattern by name and has a -Server parameter will now implicitly have 'mydc.mydomain.tld' bound to the -Server parameter unless an argument is explicitly passed.
In other words: next time you invoke Get-ADUser rsterba, PowerShell now calls Get-ADUser rsterba -Server 'mydc.mydomain.tld' instead.
For more information about $PSDefaultParameterValues and how it works, see the about_Parameters_Default_Values help topic

How to view categories in Powershell v5?

im running powershell v5 on my machine and i can't seem to run the command
GET-HELP -Category Provider.
Is there an alternative to this command which can be used in v5 or is it a command that's available to v3 Powershell?
While Provider is a valid category for help topics, none of the topics that ship with PowerShell use category Provider (anymore), as of Windows PowerShell 5.1 / PowerShell (Core) 7.2.x
See GitHub issue #8030
The next best thing is to use a wildcard-based search, using the Get-Help's (positionally implied) -Name parameter:
Get-Help *provider*
This will list all topics with the word provider in the name, which comprises both cmdlets with the word in the name and conceptual help topics (topics whose name starts with about_).
If you want to limit the output to matching conceptual help topics (as Get-Help -Category Provider may have done in Windows PowerShell versions prior to v5.1):
Get-Help *provider* -Category HelpFile
# Alternative:
Get-Help about_*provider*
[1] The valid categories are: Alias, All, Class, Cmdlet, Configuration, DefaultHelp, DscResource, ExternalScript, FAQ, Filter, Function, General, Glossary, HelpFile, Provider, ScriptCommand, which correspond to the values of a non-public enumeration type, System.Management.Automation.HelpCategory; you can obtain these values programmatically with (TabExpansion2 'Get-Help -Category ' -cursorColumn 19).CompletionMatches.CompletionText.
The topics that ship with Windows PowerShell v5.1 / as of PowerShell (Core) 7.2.x span the following categories: Alias, Cmdlet, ExternalScript, Filter, Function, HelpFile, as obtained with (Get-Help *).Category | % ToString | Sort-Object -Unique
Using Windows Powershell 5.1. when I look at help Get-Help -full, I read the following:
Parameters
-Category <System.String[]>
Displays help only for items in the specified category and their aliases. Conceptual articles are in the HelpFile category.
Required? false
Position? named
Default value None
Accept pipeline input? False
Accept wildcard characters? false
If I do a Get-Help * | Group-Object Category | Select-Object Name, I only see the following categories:
Alias
Function
ExternalScript
Cmdlet
HelpFile
I get the same categories in PowerShell v7.2

Why would $env:username and [environment]::username return different users?

What is the difference between PowerShell's $env:username and [environment]::username and why would they potentially return different users? (I understand there are other ways to get the current user as well)
Some background:
I have an Azure Pipeline that runs a PowerShell script on a release target. The pipeline agent is configured to run under a specific service account. Part of that PowerShell script uses $env:username to assign permissions. However, permissions are assigned to the local admin account instead. If I change the script to use [environment]::username the correct service account user is given permissions.
$env:USERNAME, while predefined to reflect the current user's username, is a read-write environment variable, just like any other.
Even though it's obviously inadvisable to do so, a statement such as $env:USERNAME = 'foo' changes the value of environment variable USERNAME for the current process as well as its child processes.
This means that if environment variable USERNAME was modified earlier in the same session or in PowerShell's parent process, possibly via an explicitly specified startup environment, it no longer reflects the true username.
While I don't know why the USERNAME environment variable would differ from the real account in the case of Azure Pipelines, an example of - inadvertently - setting the wrong value is PowerShell's Start-Process cmdlet when given the -UseNewEnvironment switch: due to a bug as of PowerShell Core 7.0.0-preview.5 / Windows PowerShell v5.1, $env:USERNAME unexpectedly always reflects SYSTEM, irrespective of the actual user account - see this GitHub issue.
By contrast, [Environment]::UserName uses a different method for obtaining the current user's username, which does not rely on the value of $env:USERNAME and always reflects the true username[1].
In short: Only [Environment]::UserName reliably reflects the current user account's username.
[1] From the linked docs: "On Windows the UserName property wraps a call to the Windows GetUserName function. The domain account credentials for a user are formatted as the user's domain name, the \ character, and user name. Use the UserDomainName property to obtain the user's domain name and the UserName property to obtain the user name.
On Unix platforms the UserName property wraps a call to the getpwuid_r function.
$env: is looking at your environmental variables. The variable that you're looking at- username- happens to be set to the local username by default. But it doesn't have to be.
[Environment] is a call to the System.Environment class in .NET Framework. This class has a property called UserName that contains the username that is logged into the computer.
Similiar names, but very different things. You could get the path statement by $env:Path, for example, or call [Environment]::Version to get the version of .NET Framework.
Note in this example I deliberately messed with the local environmental variable prior to launching PowerShell to give the wrong username.
C:\WINDOWS\system32>echo %username%
mspow
C:\WINDOWS\system32>set username=bob
C:\WINDOWS\system32>echo %username%
bob
C:\WINDOWS\system32>powershell
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
PS C:\WINDOWS\system32> echo $env:username
bob
PS C:\WINDOWS\system32> [Environment]::Username
mspow
PS C:\WINDOWS\system32> echo $env:ProgramFiles
C:\Program Files
PS C:\WINDOWS\system32> echo $env:path
C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\dotnet\;C:\Users\mspow\AppData\Local\Microsoft\WindowsApps
PS C:\WINDOWS\system32> [Environment]::Version
Major Minor Build Revision
----- ----- ----- --------
4 0 30319 42000
You can see all of the available environment variables by using
Get-Item -Path Env:
The only way I know of to see all of the Methods and Properties for [Environment] is the link to the MSDN site. Get-Member doesn't work on it.

How to run powershell script against domain?

I am trying to run the following powershell command through my application using C#
Get-ADUserResultantPasswordPolicy user1
It returns the values for user1 on domain1 which is my current domain.
when I try to read the values for a user2 on domain2:
Get-ADUserResultantPasswordPolicy domain2\user2
its throwing exception
"Cannot find an object with identity:'user2' under:'DC=domain2,DC=com'.
Is there away to point powershell to the other domains and read the data on that domain?
Use the -partition parameter:
Specifies the distinguished name of an Active Directory partition. The distinguished name must be one of the naming contexts on the current directory server. The cmdlet searches this partition to find the object defined by the Identity parameter.
The following two examples show how to specify a value for this parameter.
-Partition "CN=Configuration,DC=EUROPE,DC=TEST,DC=CONTOSO,DC=COM"
-Partition "CN=Schema,CN=Configuration,DC=EUROPE,DC=TEST,DC=CONTOSO,DC=COM"
Read more here: https://technet.microsoft.com/en-us/library/ee617255.aspx?f=255&MSPPError=-2147217396
You can use the -Server parameter with the fully qualified domain name of the domain controller on domain you want to access.
Get-ADUserResultantPasswordPolicy -Identity "USER1" -Server "DC1.YourDomain.com"

Specify domain controller with get-aduser in powershell

Get-ADUser -identity $ntaccount1 -properties name, samaccountname, mail, enabled, passwordlastset
Is it possible, when looking up the user account information in powershell, to specify a domain controller to use? We have some DC's that get the 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 that this is a bit of an old question, but I would like to expand on the answer given, to aid anyone else who had a similar query.
The following allows you to define a specific Domain Controller, which the entire of a script would be able to use... Why might you want to do this when the -server parameter is available to Get-ADUser, New-ADUser, Set-ADObject, etc?
Well I put together a script that creates an AD user, sets multiple properties and creates an exchange mailbox - However, one set of properties revolves around the RDS properties on a 2008 R2 user account, which 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 is no parameter setting for -server that I'm aware of.
This in itself wouldn't be a big deal, but the script also creates an Exchange mailbox for the user. As my Exchange server is in different AD Site from my workstation, the user account gets created on my local DC, but the mailbox isn't set, because the DC in the same site as the Exchange server hasn't yet received a replicated copy of the new user account.
The solution I found is as follows and is courtesy of http://www.joseph-streeter.com/?p=799
Having loaded import-module activedirectory, you'll have access to AD options in the New-PSDrive commandlet which among everything else 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 then change the working Provider with the following command.
CD <<NameofYourChoice>>:
To view the existing list of Providers, type Get-PSDrive. AD is the default Active Directory Provider created when using the ActiveDirectory commandlet. You should also see your newly created Provider.
So for instance 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:
All further active directory related commands in the script or the active shell will work with the new Provider RemoteAD. If I would need to change back to my original Provider, I'd simply type
CD AD:
Hope someone finds this useful...
This is what i use:
Get-ADUser -server dcservername.domain.local -identity username