I am still rather new to PowerShell and saw the two examples below and was wondering why they show two different results when, at least to me, they're seem to query for the same answer. Thanks ahead for the input.
get-host | select version
Vs.
(get-host).version
Get-Host | Select-Object version creates a custom object with a single property, Version, from the inputobjects which in this case is the output from Get-Host.
PS C:\Users\frode> Get-Host | Select-Object Version | Get-Member -MemberType Properties
TypeName: Selected.System.Management.Automation.Internal.Host.InternalHost
Name MemberType Definition
---- ---------- ----------
Version NoteProperty version Version=5.0.14257.1000
(Get-Host).Version loops through the objects returned from Get-Host and extract/expands the value of the Version-property. This is the same as running Get-Host | Select-Object -ExpandProperty Version or Get-Host | Foreach-Object { $_.Version }.
PS C:\Users\frode> (Get-Host).Version | Get-Member -MemberType Properties
TypeName: System.Version
Name MemberType Definition
---- ---------- ----------
Build Property int Build {get;}
Major Property int Major {get;}
MajorRevision Property int16 MajorRevision {get;}
Minor Property int Minor {get;}
MinorRevision Property int16 MinorRevision {get;}
Revision Property int Revision {get;}
Related
i need help with a powershell script that helps find if there is any duplicate attribute values for example employeeid and employeenumber having same value like 12345 in an AD environment.
i have tried
Get-ADUser -Filter {(employeeID -like "*")} -property employeeID |Group employeeID | ? {$_.Count -ge 2} | select -ExpandProperty group | Select-Object Name, Employeenumber, employeeID
The following piece of code works, but only for today's date. What do I have to change to put in a specific date, e.g. the 3rd of May 2022? I also want to do that with a Read-Host.
Get-ChildItem -Path C:\test\testdir -Include *.txt, *.log -Recurse |
Where-Object LastWriteTime -ge ([datetime]::Today) |
Sort-Object LastWriteTime |
Select-String -Pattern Test
(get-item "C:\tmp\test.xml").lastwritetime.gettype()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True DateTime System.ValueType
The datatype is DateTime, so no need to build matching strings.
In regards to the requirement to use read-host you could do:
[datetime]$DateTime = read-host
By doing so you cast the Input as DateTime but this requires that you enter a supported format. I think its easier if you have to specify the days back to calculate the DateTime information, e.g.;
[int]$daysBack = read-host
Get-ChildItem -Path C:\test\testdir -Include *.txt, *.log -Recurse |
Where-Object LastWriteTime -ge (get-date).AddDays(-$daysback) |
Sort-Object LastWriteTime |
Select-String -Pattern Test
I want to output the last entry of Get-Hotfix in PowerShell. How does it work?
Example:
Source Description HotFixID InstalledBy InstalledOn
------ ----------- -------- ----------- -----------
DESKTOP-FU... Update KB5010472 NT-AUTORITÄT\SYSTEM 15.03.2022 00:00:00
DESKTOP-FU... Update KB5003791 06.10.2021 00:00:00
DESKTOP-FU... Security Update KB5011487 NT-AUTORITÄT\SYSTEM 14.03.2022 00:00:00
DESKTOP-FU... Update KB5007273 NT-AUTORITÄT\SYSTEM 02.01.2022 00:00:00
DESKTOP-FU... Security Update KB5011352 NT-AUTORITÄT\SYSTEM 09.02.2022 00:00:00
DESKTOP-FU... Security Update KB5005699 06.10.2021 00:00:00
I want to output the KB5005699 update to the console.
You can use Select-Object with the -Last 1 parameter to get the last object and -ExpandProperty HotFixID to get the Value of the HotFixID property:
Get-HotFix | Select-Object -Last 1 -ExpandProperty HotFixID
Another alternative would be to get the last object by the -1 index and use dot notation .HotFixID to get the Value:
(Get-HotFix)[-1].HotFixID
gwmi win32_quickfixengineering InstalledOn |sort installedon -desc | select -First 1
I've tried but that gives me an odd formatting. I simply need to return the installedOn date and maybe the name.
i'm trying to find the most recently installed patch on a system and get that date value.
I've also tried.
gwmi win32_quickfixengineering |sort installedon -desc | select -First 1
better formatting but still too much information.
To get just the InstalledOn properties, you can use the -ExpandProperty option as below.
GWMI win32_quickfixengineering | sort -Descending InstalledOn `
| Select -ExpandProperty InstalledOn -First 1
Otherwise you can do something like the below to get a brief overview of the installed KBs.
GWMI win32_quickfixengineering | Select HotFixID, InstalledOn, Caption
Info:
Select -ExpandProperty
Get-HotFix / win32_quickfixengineering
The easiest way to get specific information you want is the following:
Lets say you need a name of a service for a command:
$Service = Get-Service Spooler
That command would retrieve more information than just the name, but to get the only name, you can type:
$Service.name
That will return only that value from the saved variable.
So in your case you could do:
(Here you save all the information into a variable)
$Quickfix = gwmi win32_quickfixengineering | sort installedon -desc | select -First 1
(You can then use that variable to return specific information from that variable)
Then use: $Quickfix.InstalledOn
That will return you JUST the date.
Then you could do something like:
Write-Output "Latest update was $($Quickfix.HotFixID) installed on: $($Quickfix.InstalledOn)"
That will give you an output of the KB and the date installed. You can of course edit the text, it was just an example.
I am having an issue when setting labels text with a PowerShell command. I am trying to set a label to the size of the mailbox, this is the command I use.
$MailBoxSize.Text = Get-Mailbox -Identity $comboBox1.SelectedItem | Get-MailboxStatistics | Select TotalItemSize | ft -HideTableHeader
Here is the result I get.
Microsoft.PowerShell.Commands.Internal.Format.FormatStartData Microsoft.PowerShell.Commands.Internal.Format.GroupStartData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.GroupEndData Microsoft.PowerShell.Commands.Internal.Format.FormatEndData
You should never be capturing the output of a Format-Table (or any other Format- command). Those commands are used for formatting output, usually to the console. Instead, if you wish to get the value to a property use the -ExpandProperty parameter of the Select-Object cmdlet. Change your line to read:
$MailBoxSize.Text = Get-Mailbox -Identity $comboBox1.SelectedItem | Get-MailboxStatistics | Select -ExpandProperty TotalItemSize