An example
get-process | select-object vm, cpu,id | out-gridview
Here select-object actually is selecting PROPERTIES of the OBJECTS in the COLLECTION stored in memory.
Anyone knows why this cmdlet is not called select-property ?
Maybe I m asking a noob quesiton......
When you call Select-Object Blah, Blah1, Blah2, you are using the Property parameter via positional binding
Notice that the help describes a lot more than just the -Property parameter you are using:
Get-Help Select-Object -Full
<#
...
The Select-Object cmdlet selects specified properties of an object or set
of objects. It can also select unique objects, a specified number of
objects, or objects in a specified position in an array.
To select objects from a collection, use the First, Last, Unique, Skip, and
Index parameters. To select object properties, use the Property parameter.
When you select properties, Select-Object returns new objects that have
only the specified properties.
#>
Spend some time using the built in help system, there is a trove of information in there!
#Get the full help details
Get-Help Select-Object -Full
#If available, look online for help
Get-Help Select-Object -Online
#List conceptual topics
Get-Help about_*
#Read about regular expressions
Get-Help about_Regular_Expressions
Lastly, if you're talking semantics, the noun in a PowerShell command name refers to what your verb is acting upon: objects.
Cheers!
Related
I'm not very clear on how the #{n=;e=} construct works in PowerShell.
Does this type of thing have a name that I can find examples from?
For example, I find examples like this that works great:
gwmi win32_logicaldisk | Format-Table DeviceId, VolumeName, #{n="Size(GB)";e={[math]::Round($_.Size/1GB,2)}},#{n="Free(GB)";e={[math]::Round($_.FreeSpace/1GB,2)}}
When I try to do something like that, I can never get it to work. This works fine:
Get-Command -Module Microsoft.Powershell.Utility | Where CommandType -eq Function | Select Name,Version,CommandType
So I thought I would try and add the definition of that function to a new column using cat function:\$_.Name
Get-Command -Module Microsoft.Powershell.Utility | Where CommandType -eq Function | Select Name,Version,CommandType,#{n="Contents"; e={cat function:\$_.Name}}
But I just get an empty Contents column :(
Can someone give me some pointers on how the #{n=;e=} construct works?
Also, what do the n and e stand for?
#{n='';e={}} syntax is called a calculated property. n stands for Name and e stands for expression. You can even specify Name instead of n and Expression instead of e.
Calculated properties allow you to create new properties or change existing ones. This is done by passing a special hashtable to the Property parameter rather than a static property name. This is a useful feature where you create new properties using custom expression in a script block and use existing properties.
Not for only Select-Object this work for also Format-Table, Format-List cmdlets. These don't work outside this cmdlets.
Calculated properties are a quick way to manipulate command output to return just about anything you like. These save your time and reduce code length.
Sidenote: The last code in your question dosent work because you need to join two path using Join-Path. Calculated properties are innocent here. Even you can join path like this: cat "Function:\$($_.Name)" as #MathiasR.Jessen pointed.
#{} is a hashtable. {} is a scriptblock inside the hashtable set equal to e. The hashtable is used in a custom way for select-object.
$scriptblock = { $_ }
$hashtable = #{ name = 'number'
expression = $scriptblock
}
1..3 | select-object -property $hashtable
number
------
1
2
3
When I use the Select-Object Name CmdLet it seems to create a new object with a single Name property on it.
I often want to pipe this selection to other CmdLets but they often take just a string.
How can I easily get a bunch of objects and say "Select only property x and just the property values into an array or collection of just its values"?
You can use the ExpandProperty parameter for this. This switch means that instead of returning an object with properties as listed on the (default) -Properties parameter, the value of the single property listed under -ExpandProperty parameter is returned.
NB: You can also use the alias, expand for this parameter.
Example:
Get-Process | Select-Object -ExpandProperty ProcessName
Related documentation:
SS64: https://ss64.com/ps/select-object.html
MS Docs: https://learn.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/Select-Object?view=powershell-3.0
I have an example code snippet that suggests using
(Get-Process | Where-Object {$_.WorkingSet64 -gt 20mb}).Count
to return the count of all processess using > 20Mb.
It works, but when typing, neither Intellisense or the "Tab" key shows this property, rather they show the properties of an individual process - which I find misleading.
I understand, that specifying an item property will give me the list of that property only, but is there a way to easily see, in general, what ALL the valid propeties are, including list aggregates etc?
Even assigning to a variable
$processes = Get-Process | Where-Object {$_.WorkingSet64 -gt 20mb}
does not show me "Count" as a valid property of $processes until AFTER the assignment has been actually run and the value assigned - when writing the script it still shows the properties for an individual item.
For me, Intellisense / Tab help that does not cover all the options kind of defeats the purpose ... (not having to remember hundreds objects/functions and their properties / parameters).
Is there any way to improve this situation? Some syntax trick have I missed?
The correct way to find out all of the properties of an object is to pipe the output to Get-Member:
Get-Process | Get-Member
Sometimes there are hidden properties and methods that can only be seen if you add the -force switch:
Get-Process | Get-Member -Force
The count property is an automatic property that is always usable on any collection object but that isn't explicitly listed as a property. Another example of an automatic property is length.
Using #() to force an array type is handy when that is what is wanted.
e.g. $processes = #(Get-Process | Where-Object {$_.WorkingSet64 -gt 20mb}). will show you "Count" and the other array properties.
Other than that, let's say the Intellisense has various limitations / shortcomings that I will just have to learn... sigh.
I`m new to PowerShell and was looking for a way to retrieve the properties of an object. I read up on Get-Member.
However, when I tried to get the properties for an object "created" by the SharePoint Online cmdlet (Remove-SPOSitGroup) I get an error message:
"get-member : you must specify an object for the get-member cmdlet"
Moreover, in order to be even able to pipe the cmdlet to Get-Member I have to specify the required parameters "site" and "identity" for the cmdlet.
This in turn executes the command and deletes a SharePoint group.
I don`t want that. I just want to get the properties of the object that Remove-SPOSitGroup produces.
Here`s my command:
Remove-SPOSiteGroup -Site XXX -Identity XXX | Get-Member -MemberType Property
This works fine with
Get-Command | Get-Member -MemberType Property
As I mentioned this is new to me so any help is appreciated.
Thanks.
The issue what you are having is because you command is not giving any output:
Remove-SPOSiteGroup's output will pass as an Input to the next cmdlet if you are piping it. Thats what Pipe symbolizes in PS.
So, in your case the return type is nothing as a result, the next cmdlet which is Get-Member is not getting any object to give you the details of methods & properties. Hope you are clear now about get-member
I have a powershell script containing several functions representing tasks. I would like to format a list of those functions with for each function its name and the synopsis provided in the documentation.
For one function Foo:
Get-Help Foo | Format-Table -Property Name, Synopsis
I don't know how to make it work with multiple functions. I am having troubles with the power syntax. I don't know how to declare a list of function because Foo, Bar is a syntax error. I also tried to list the names of the function as strings and convert those to the corresponding objects but I failed to do so.
How to print the name and synopsis for a custom list of functions?
As you figured out yourself, the Get-Help (alias help) cmdlet doesnt expose a parameter set with multiple -name parameters. However, You can define an array of your functions, iterate over it and call the Get-Help for each of them. Example:
#('Get-Content', 'Get-ChildItem') | foreach { help $_ } | Format-Table -Property Name, Synopsis
Output:
Name Synopsis
---- --------
Get-Content Gets the content of the item at the specified location.
Get-ChildItem Gets the items and child items in one or more specified locations.