I have learned the basics of using UNIX but I am now trying to learn how to use powershell. Most of the commands are similar or easy to find the equivalent of. However, I have not found the equivalent of whatis. Is there an equivalent? I have tried searching online but the search engines take "whatis" as being "what is". Is there anybody that knows what the equivalent is or if there is an equvalent at all?
use Get-Help to display help about cmdlets and concepts. It is your best friend when you can't remember commands
Get-Help <cmdlet>
I would also suggest Get-Alias to see which UNIX commands map to PowerShell cmdlets
You can even create you own aliases. You can add whatis to map to get help.
New-Alias whatis Get-Help
which would then allow you to do
whatis <cmdlet>
For good measure, the second best command to get familiar with is
Get-Command
Using Get-Command along side Get-Help will help (pun intended) you get up to speed with PowerShell. I do suggest using a filter with Get-Commad as the list tends to be very long. It allows wildcard filters
Get-Command *-command
Related
My understanding is that splatting variables is the preferred/recommended way to make longer function calls in PowerShell scripts. However, I use vscode as my primary IDE and understandably, extracting the parameters into a hashtable and splatting them makes intellisense unusable.
Is there any library/framework/vscode extension for splatting that allows the use of intellisense by way of naming convention or something like that?
I know this is a relatively old question but whilst looking for similar I came across Editor Services Command Suite which looks like it may be useful. It allows you to write the command out and then convert it to a splatted version:
ESCS github repo
It was this blog post by Rob Sewell (sqldbawithabeard) which brought it to my attention: blog post
9/10 times if you are trying to use the Invoke-Expression cmdlet, there is a better way. Building the arguments to a command dynamically? Use an array of arguments. Building the arguments to a cmdlet? Use splatting with an array or hashtable. Your command has a space in the path to it? Use the call operator (&).
This might seem open ended, but Invoke-Expression is an easily accessible cmdlet where the answer is almost always to never use it. But the cmdlet exists for a reason, is not deprecated, and most criticisms of its use state something similar to, "it's almost never the right answer", but never states when it is acceptable to use it. In what case is it acceptable to use Invoke-Expression? Or to word it a bit less openly, how was Invoke-Expression designed to be used?
To quote from a PowerShell team blog post titled Invoke-Expression considered harmful (emphasis added):
The bottom line: Invoke-Expression is a powerful and useful command for some scenarios such as creating new scripts at runtime, but in general, if you find yourself using Invoke-Expression, you should ask yourself, or maybe a respected colleague if there is a better way.
EBGreen notes:
Or to phrase it another way, It [Invoke-Expression] is ok to use as long as a user is never involved in any part of generating the string that will be invoked. But even then, not using it will enforce better habits than using it would.
In short:
As a matter of habit, always consider a different (usually more robust and secure) solution first.
If you do find that Invoke-Expression is your only choice, carefully consider the security implications: if a string from an (untrusted) outside source (e.g., user input) is passed directly to Invoke-Expression, arbitrary commands may be executed.
Therefore: Only use Invoke-Expression if you fully control or implicitly trust the input.
Note: As of Windows PowerShell v5.1 / PowerShell Core v6.1.0, the official Invoke-Expression help topic doesn't provide such guidance; this GitHub issue suggests rectifying that.
Rare examples of justified (safe) use of Invoke-Expression:
Creating PSv5+ custom classes dynamically:
so that the class can be used in a remote session.
so that the set of properties can be created based on conditions at runtime.
Using Invoke-Expression in combination with Write-Output:
to parse a string with embedded quoting, with extra precautions.
to parse command lines stored in a file, if trusted.
Using Invoke-Expression for nested property access:
via a property path stored in a string.
In PowerShell 3 if you are searching for a command you could use both, both Get-Help Get-* and Get-Command Get-* work?
So whats the major difference ?
Both commands share a lot of information in common but the main difference is that Get-Help outputs MAML objects (which are "text based", error prone and even can be out dated) while Get-Command gets you real objects (metadata), that you can further investigate.
For most help parts, Get-Help is displaying pre-made help, contained in XML files.
For other parts, Get-Help "is using" Get-Command to generate the information, like the SYNTAX section.
Get-Command also gets you information that Get-Help doesn't, like the module of the command, , it's DLL path (in case of a compiled cmdlet), parameter sets, and so on.
One is not a replacement for the other, you use both under different circumstances.
The way I think of it is - Get-Command returns the technical information about commands (DLL, implementing type, function body for functions, etc), Get-Help returns the user-friendly information about commands (detailed syntax, examples, explanation of parameters, etc).
And Get-Command returns a normal object, which behaves perfectly normally and predictably, whereas Get-Help returns a weird formatted help object which is really only intended for viewing in the console, not for processing in code.
Reading the Microsoft documentation for creating PowerShell Cmdlets, I notice there is no example code for F#; instead, there is a message saying that "This language is not supported or no code example is available.".
Is F# not supported for creating Cmdlets, or aren't there any examples in F#?
Take a look at this:
Writing PowerShell Cmdlets in F#. Not exactly a complete example of how to do this but it certainly seems to be possible to write cmdlets in F#.
EDIT:
There is a more extensive example here but the contextual information is a little bit out of date. Actually the contextual information is a bit out of date on both links but the first link is more recent.
Approaching cmdlets in a conceptual way,
How are they made? Are they compiled?
Is it the equivalent of a batch file for PowerShell? Is it a script or a binary?
What is the structure used for storing these cmdlets?
A PowerShell cmdlet is a compiled piece of .NET code, more precisely a single class if I am not mistaken. Cmdlets are kind of the "native" commands in PowerShell land, being able to handle object input and output as well as usually playing nice and well with the (object-based) pipeline.
Cmdlets have no direct representation in the file system, as they are not programs or similar. They exist solely within PowerShell. You can use the Get-Command cmdlet to query all available cmdlets, functions, etc.
You can write cmdlets with a .NET language, such as C#. With PowerShell v2 there is also the possibility to write so-called advanced functions which behave similarly to cmdlets and have comparable capabilities but are interpreted PowerShell code, instead of compiled classes. This may incur a run-time overhead.
This link may help in understanding powershell cmdlet:
http://www.powershellpro.com/powershell-tutorial-introduction/tutorial-powershell-cmdlet/
See Scripting with Windows PowerShell.
A PowerShell cmdlet is a user-created extension to the PowerShell scripting language. The Cmdlet itself is a .NET class extending from PSCmdlet. Usually, additional components are included with the cmdlet to provide help and registering the cmdlet.
A cmdlet allows you to access to all functions accessible through the .NET virtual machine. This can range from simple script aids to fully functional programs.