How to view categories in Powershell v5? - powershell

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

Related

How can i list namespaces under the root?

I want to list which things are useable under \\root\ .However i don't know how to list or see which things i can use.Because,i am beginner in the powershell.
I am coding this thing:
wmic /namespace:\root\
(But i don't know which things i can use under root.And because of this, i cannot use anything :/)
How can i list which things could be useable under root ?
If someone can help, i will be really happy :D
I tried use "/?" but it didn't help.Also i researched on google BUT again i couldn't find something useful for myself or maybe i couldn't understand their solutions.
There is a WMI class of __namespace you can query:
Get-WmiObject -Namespace Root -Class __Namespace | Select Name
Name
----
subscription
DEFAULT
CIMV2
msdtc
Cli
SECURITY
SecurityCenter2
RSOP
PEH
StandardCimv2
WMI
MSPS
directory
Policy
Interop
Hardware
ServiceModel
SecurityCenter
Microsoft
Appv
I would recommend reading through about WMI. It covers some of the discoverability aspects, which are important because:
In a default installation of Windows 8, there are more than 1,100 WMI classes in Root/Cimv2
Newer versions of powershell use CIM over WMI with commands like Get-CimInstance. It's not worth worrying about for now, but it's good to look into while you're learning
WMIC is a separate exe from powershell, and doesn't return powershell objects. I would avoid it unless you're stuck to command prompt
Cpt.Whale's answer is helpful, but is uses the deprecated WMI cmdlets (from the Get-WmiObject docs page: "Starting in PowerShell 3.0, this cmdlet has been superseded by Get-CimInstance"); similarly, wmic.exe is deprecated (see note at the top of the page). Both are deprecated in favor of the CIM cmdlets, so the standard advice applies:
The CIM cmdlets (e.g., Get-CimInstance) superseded the WMI cmdlets (e.g., Get-WmiObject) in PowerShell v3 (released in September 2012). Therefore, the WMI cmdlets should be avoided, not least because PowerShell (Core) v7+, where all future effort will go, doesn't even have them anymore. Note that WMI still underlies the CIM cmdlets, however.
For more information, including the differences between these two sets of cmdlets, see this answer.
Thus, here are solutions based on the CIM cmdlets:
To get all namespaces nested inside another namespace, such as root:
Get-CimInstance -Class __Namespace -Namespace root | ForEach-Object Name
To get all classes inside a given namespace, such as root, by name:
Get-CimClass -Namespace root | ForEach-Object CimClassName
Note:
Append | Sort-Object to the commands above to get alphabetically sorted output.
The default namespace (also for the WMI cmdlets) is root/cimv2, which applies if you omit a -Namespace argument.

power shell script output to txt file

This works perfectly on Windows 10 using powershell but fails badly on Win Server 2012 R2.
I have tried many forms using redirect and -outfile but at best end-up with an empty file or broken script.
Can any one help me send the one liner below to a txt file on win server 2012 R2, I'm just not getting it
$((Get-WmiObject -Query "select Name, PercentProcessorTime from Win32_PerfFormattedData_PerfOS_Processor") | foreach-object { write-host "$($_.Name) : $($_.PercentProcessorTime)"}) *>&1 > output.txt
Write-Host, as the name implies, writes to the host, which in a console window is that window's display (screen), bypassing PowerShell's (success) output stream, the latter being what the pipeline operator (|) and the redirection operator > operate on.
In PowerShell v5+ only, Write-Host writes to the information output stream (stream number 6 - see the about_Redirection help topic; by default, that output still goes to the host) and can therefore be redirected - either via 6> or via *> - so the catch-all redirection *>&1, which redirects all streams to the success output stream (1), can indeed be used to redirect Write-Host to the success output stream, but not in earlier PowerShell versions - and Windows Server 2012 R2 shipped with PowerShell version 4.
However, in your case there is no good reason to use Write-Host to begin with: either use Write-Output - the cmdlet whose purpose is to write to the success output stream (1) - or, preferably, use PowerShell's implicit output feature, where any output (return value) not captured in a variable, piped or redirected is implicitly written to the success output stream:
# Note how the use of "$($_.Name) : $($_.PercentProcessorTime)"
# *by itself* implicitly causes it to be *output* (written to the pipeline).
Get-WmiObject -Query "select Name, PercentProcessorTime from Win32_PerfFormattedData_PerfOS_Processor" |
Foreach-Object { "$($_.Name) : $($_.PercentProcessorTime)" } > output.txt
Note the absence of $(...), the subexpression operator in the command, which isn't needed.
If the specific spacing between the columns isn't important, you can more simply write (since only 2 properties are being select, implicit Format-Table formatting is applied):
Get-WmiObject -Query "select Name, PercentProcessorTime from Win32_PerfFormattedData_PerfOS_Processor" |
Select-Object Name, PercentProcessorTime > output.txt
Note: The CIM cmdlets (e.g., Get-CimInstance) superseded the WMI cmdlets (e.g., Get-WmiObject) in PowerShell v3 (released in September 2012). Therefore, the WMI cmdlets should be avoided, not least because PowerShell [Core] (version 6 and above), where all future effort will go, doesn't even have them anymore. For more information, see this answer.
Try this... note that it is appending to the file, so if you run it twice, you will have two runs' worth of data
$((Get-WmiObject -Query "select Name, PercentProcessorTime from Win32_PerfFormattedData_PerfOS_Processor") | foreach-object { "$($_.Name) : $($_.PercentProcessorTime)" >> output.txt})
EDIT
OP accepted this as the answer, but it's not necessarily the best solution. For a thorough explanation and another solution, I recommend a different answer submitted for this question.

Get-Help shows different outputs on different systems

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

Get Parameter Names used by PowerShell functions

In the past week I have been looking at some Powershell best practices. One of these is to re-use Powershell parameter names so commands feel more intuitive.
Does anyone have a function for retrieving parameter names for all functions?
You can use the Get-Command cmdlet to retrieve all commands and select the parameter names:
get-command | % { if ($_.Parameters) {$_.Parameters.Keys }} | select -Unique | sort
jisaak's post will get you a list of commands as requested; the best practices I read recommended re-using the Powershell verbs which you can get by:
Get-Verb
Modules containing modules that don't use the standard verbs generate a nice error:
WARNING: The names of some imported commands from the module '***' include
unapproved verbs that might make them less discoverable.
To find the commands with unapproved verbs, run the Import-Module command again
with the Verbose parameter. For a list of approved verbs, type Get-Verb.

How to find new cmdlets in Powershell v3.0

I wanted to find the new cmdlets / functions in Powershell. I used the following approach, but not sure if it is comprehensive / correct. Any ideas to find this in a better / different way?
Run the below once from v2 and once from v3 ( and write to a different file)
get-command -Module Microsoft.PowerShell.* |
select -expand name | out-file e:\poshv2.txt
Then use Compare-Object to see what's added ( or removed)
Compare-Object (gc e:\poshv2.txt) (gc e:\poshv3.txt)
My observation based on this is that there were 25 new cmdlets added ( and none were removed)
One question that was raised as a comment on my blog was that Disable-PsRemoting, which appeared in this list, is not really new. The reason it appeared was that it was not in the modules under Microsoft.Powershell.* ( and it was not a cmdlet), but it is in v3.0.
The only difference which you already noted is that in v2 Disable-PsRemoting was a function and in v3 it's a cmdlet. I wrote about cmdlet and parameter changes in v3 (using a similar compare method) on the PowerShell Magazine website.
http://www.powershellmagazine.com/2011/09/15/how-to-find-out-whats-new-in-powershell-vnext/