I am trying to figure out something in powershell using piping.
I have something like this:
Get-Project -All | %{ $_.Name }
Note: I know %{} = ForEach{}
Get-Project -All is a NuGet command which returns all project in current solution.
This works for me, but I would like to find out all available variables that get piped through. I only know $_.Name is one of them as I can see from an example online.
When I run Get-Project -All I get the following output
ProjectName Type FullName
----------- ---- --------
ABC C# C:\SolutionDir\Category\ABC.csproj
Now inside my pipe I found the following variables:
$_.Name = ABC
$_.ProjectName = Category/ABC
$_.Type = C#
$_.FullName = C:\SolutionDir\Category\ABC.csproj
Now what I don't understand is
The "Table" output of Get-Project -All has a header of ProjectName with value of ABC. But inside the pipe $_.ProjectName = Category/ABC and$_.Name = ABC`. Are they unrelated?
If I didn't see the example online mentioning $_.Name I would of never known about this variable. Is there some way of getting all available variables?
Note:
Get-Project -All | %{ Get-Variable } does not return any of the above mentioned variables.
When you use the pipeline, it's the entire object created, in this case Get-Project, that is sent through it. To see all members of an object, use Get-Member.
So in your case, you can use Get-Project -All | Get-Member to see all the properties and methods of the object being returned.
Related
I would like to export some Server Statistics from a Failover Cluster System.
My plan is to show with the Get-VHD command all VHD used on the VMs on my host.
So I was Trying to use:
Get-VM | select-object VMID |get-vhd |ft
This gives me a list the
"ComputerName, Path, VHDFormat,VHDType,FileSize,MinimumSize, LogicalSectorSize, PhysicalSectorSize"
Sadly the ComputerName is not the VMName but just the Name of the Host.
Now when I run the Get-VM command I get the Name and here it is actually the VMName.
Is there a nice way to get the real VMnames in the Output of Get-VHD?
I am fairly new to Powershell and I could not find a solution that worked... Most of the "sniplets" found here did not work at all or did not return the VMname...
Thank you for your suggestions =)
Sorry, I think my Title is not that well described, I was not sure how this funktion is called.
Untested, but you could use a ForEach-Object loop and capture the Name property from the Get-VM cmdlet. Then go on with Get-VHD and combine the output:
Get-VM | ForEach-Object {
$name = $_.Name
$_ | Get-VHD | Select-Object #{Name = 'Name'; Expression = {$name}}, *
} | Format-Table -AutoSize
I have a following piped command that should print a GPO name and the group that is associated with it, repeating this untill all GPOs and groups have been printed. The output isn't right though. The name comes out correctly but the group says Microsoft.GroupPolicy.GPTrustee instead of the groups name.
How should I access it to get the value?
Here is my piped command:
Get-GPO -All | ForEach-Object {$gpo = $_.DisplayName; Write-Output $_;} | Get-GPPermission -All |
Where-Object {$_.Permission -eq "GpoApply"} |
Select-Object #{Name="GpoName"; Expression={$gpo}},#{Name="Group"; Expression={$_.trustee}}
EDIT: The problem was solved, but I'll still copy the output of "Microsoft.GroupPolicy" -object here to clarify why I thought I'd be able to access it with $_.trustee variable.
It looked like this so $_.trustee.name didn't even cross my mind.
Trustee : Domain Computers
TrusteeType : Group
Permission : GpoApply
Inherited : False
I'm trying to sort the AWS Instances I pull with the Get-EC2Instance cmdlet but the issue I'm facing is that the property is a Tag and I'm not sure how to format it properly. I only know about assigning simple properties like "Sort-Object -Property Name".
I used the following to get the AWS Instances filtered by the Name tag.
$ids = Get-EC2Instance -Filter #( #{name='tag:Name'; values="*EXAMPLE*"}) | Select-Object -ExpandProperty instances | #insert sort here
Trying to pipeline sort in the last part. I tried properties like tag, tag:Name, tag:Key=Name but all failed. When I used Get-EC2Image, I had no issues with Sort Name but can't figure it out for Get-EC2Instance.
There is a AWS CLI version and answer at Sort EC2 Instances by Tag Name but I wasn't able to apply it to PowerShell.
EDIT: Rewrote question and added more details since it got downvoted.
You can pipe the output to Sort-Object cmdlet. I dont have access to a AWS instance to test this. But try variation of this command
$ids = Get-EC2Instance -Filter #( #{name='tag:Name'; values="*EXAMPLE*"}) | Select-Object -ExpandProperty instances | Sort-Object $_.Tag.Value
There are two ways. You can get the object using the $_
OR
You can reference the property directly by using a method chain like:
(Get-EC2Instance).instances.tag.value
I'm trying to get into PowerShell and have encountered my first hurdle.
when I run
Get-Command | Where-Object CommandType -contains Cmdlet
My output gets filtered so that only commands with "CommandType" property value containing "Cmdlet" gets shown, like so:
Same thing can be done with the object "Source":
Get-Command | Where-Object Source -contains appx
Which gets me:
But when i try to run:
Get-Command | Where-Object Name -contains Add
I get nothing.
Why can I filter the output by the objects "CommandType", and "Source but not "Name"? I'm surely missing something here...
Edit: i know i can run:
Get-Command -verb "get"
And get the desired output. But i'm trying to figure out why my "where-object" statement did not work.
Edit 2:
Appearantly if I use the "-match" comparison operator it works...
get-command | where-object Name -match "add"
But isn't "name" properties just strings? -match should be used for Regular expression comparison afaik? I'm so confused right now...
use either the like or the match operator:
Get-Command | Where-Object Name -like Add*
this will match add anywhere in the word
Get-Command | Where-Object Name -match Add
but a better way to do this would be:
Get-Command -verb Add
read more about the contains operator here
-Contains
Description: Containment operator. Tells whether a collection of reference
values includes a single test value. Always returns a Boolean value. Returns TRUE
only when the test value exactly matches at least one of the reference values.
PS C:\> "abc", "def" -Contains "def"
True
When using powershell, sometimes I want to only display for example the name or fullname of a file.
From what I can gather, the way to do this is by using Get-ItemProperty (alias of gp) and passing -n fullname, For example
PS C:\Dev> gp . -n fullname
Notably I want to use this in longer scripts combined with foreach and where, and so on
Powershell then displays the fullname, but it also displays a bunch of other stuff, as follows:
PSPath : Microsoft.PowerShell.Core\FileSystem::C:\Dev
PSParentPath : Microsoft.PowerShell.Core\FileSystem::C:\
PSChildName : Dev
PSDrive : C
PSProvider : Microsoft.PowerShell.Core\FileSystem
fullname : C:\Dev
My question is, how can I get powershell to only display the property I want (fullname) and not cruft the display up with all the other stuff. Is Get-ItemProperty even the right way to do this?
Update:
If I do this:
ls -r | ?{ $_.fullname -match "foo" }
This gives me a series of lists, one for each directory, showing all the 'foo' files in each directory. What I'd like to do is consolidate those multiple lists into one single list, and not show the Mode, LastWriteTime, Length or any other irrelevant stuff. If Get-ItemProperty is not the correct way to show those things, what is?
There are many ways. I think you were looking for Get-ChildItem:
PS > get-childitem x.txt | select fullname
FullName
--------
C:\WINDOWS\system32\WindowsPowerShell\v1.0\x.txt
PS > get-childitem x.txt | select name
Name
----
x.txt
PS > get-childitem x.txt | % { $_.fullname }
C:\WINDOWS\system32\WindowsPowerShell\v1.0\x.txt
PS >
You could use Get-ItemProperty, but I think it's more for things like registries.
PS > Get-ItemProperty x.txt | select name
Name
----
x.txt
Format-Table, alias ft is also good to know about...
PS > gci x.txt | format-table -a fullname,length
FullName Length
-------- ------
C:\WINDOWS\system32\WindowsPowerShell\v1.0\x.txt 126
Commonly used aliases for Get-ChildItem, are ls, dir, and gci. I'd go with gci, when in Rome.
Just an additional comment... The way PowerShell displays information to the screen can also be modified by making changes to its Extended Type System (ETS).
Check the help for Update-TypeData.
Going this route does introduce some more advanced topics...
you can use (depending on what you exactly need):
(gi c:\temp\file -ea 0).fullname
(gi c:\temp\file -ea 0) | select -ExpandProperty fullname
C:\temp\file
-ea: do not show error if file does not exist
If you are in a foreach loop and you have a reference to the object that you are interested in, then have you tried something like the following to just echo the property.
foreach ($file in dir)
{
$file.fullname
}
I've always restricted my use of Get-ItemProperty for navigation providers like the registry where getting data via Get-Item or get-ChildItem. Compare
ls hklm:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
gp hklm:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
what you want to do is use the properties directly
ls | ? { $_.FullName -match "whatever"}