Can not get where-object & -gt to work together - powershell

I'm simply trying to filter out the variables of the "PeakWorkingSetSize" process so that the output will ONLY be greater than 5000. No matter what combination I try, the numbers won't filter. Please help.
get-wmiobject -class Win32_Process | where-object {$_.PeakWorkingSetSize -gt "5000"} | format-table -auto
FIXED thanks to #Matt!
I actually still don't know what the problem was. XD
Here is the code that works:
get-wmiobject -class Win32_Process | where-object {$_.PeakWorkingSetSize -gt "5000" | format-table -property Name,PeakWorkingSetSize -auto

There is nothing wrong with this query and it should run as you have it written. Format-Table would be making for some odd output size some of those properties do not translate to string very well. Your query and selecting some specific properties yields expected results for me. 5000 is a really low number though.
PS C:\Users\Matt> get-wmiobject -class Win32_Process | where-object {$_.PeakWorkingSetSize -gt "5000"} | select name,processid,peakworkingsetsize
name processid peakworkingsetsize
---- --------- ------------------
System 4 15588
csrss.exe 580 35412
csrss.exe 732 47356
services.exe 780 11820
winlogon.exe 812 8832
.... output truncated.....
If something is still wrong I think we need to see your expected output to understand what is wrong.

Related

Getting output as infinite or Nan

I have tried the below command to get the memory usage of the computer but getting error as infinite or NaN.
$Totalsizemem=gwmi Win32_PhysicalMemory | Measure-Object -Property capacity -Sum | Foreach {"
{0:N2}" -f ([math]::round(($_.Sum / 1GB),2))}
gwmi -computername localhost Win32_Process | Sort WorkingSetSize -Descending | Select
Name,#{n="Memoryusage(%)";Expression ={[math]::round(($_.WorkingSetSize / 1GB),1) /
$Totalsizemem*100}} | Select -First 10 | Format-Table -AutoSize | Out-Default
The expected output is -
Name Memoryusage(%)
---- --------------
powershell_ise.exe 0.655737704918033
explorer.exe 0.655737704918033
explorer.exe 0.655737704918033
explorer.exe 0.655737704918033
but the output i'm getting -
Name Memoryusage(%)
---- --------------
powershell_ise.exe ∞
svchost.exe ∞
explorer.exe NaN
svchost.exe NaN
explorer.exe NaN
Does anyone know how to rectify this?
You keep rounding and formatting the intermediate results, sacrificing accuracy to the point where your data is meaningless.
Keep it simple:
$Totalsizemem = Get-WmiObject Win32_PhysicalMemory | Measure-Object -Property Capacity -Sum |Select -Expand Sum
Get-WmiObject Win32_Process |Sort WorkingSetSize -Descending |Select Name,#{Name='MemoryUsage(%)';Expression={$_.WorkingSetSize * 100 / $Totalsizemem}} -First 10

How to execute several cmdlets in powershell script?

I have written this script to get some information about my Virtual Machine. When I execute this script the last two cmdlets (lines) don't execute, but when I execute them alone they run properly.
Get-CimInstance Win32_LogicalDisk -Filter "DriveType=3" | Select-Object DeviceID, SystemName,
#{n='Size (GB)'; e={$_.Size / 1GB -as [int]}},
#{n='Freespace (GB)'; e={$_.Freespace / 1GB -as [int]}};
$TotalMemory = (Get-CimInstance Win32_OperatingSystem).TotalVisibleMemorySize / (1024*1024)
$UsedMemory = (Get-CimInstance Win32_OperatingSystem).FreePhysicalMemory / (1024*1024)
$TotalMemory = [math]::Round($TotalMemory,2)
$UsedMemory = [math]::Round($UsedMemory,2)
$TotalMemory
$UsedMemory
Get-CimInstance Win32_Processor | Select-Object -Property NumberOfCores;
Get-CimInstance Win32_Processor | Measure-Object -Property LoadPercentage -Sum | Select-Object Sum;
What is the problem?
Any comments would be appreciated.
This question has been asked a million times. Format-table is being implicitly run, and it doesn't handle different sets of columns well. All the objects are there. You can pipe the whole script to format-list. You can put get-date at the very beginning. A known object with a format file at the beginning fixes it. You can output more than 4 properties with the first object. I tried to ask for a warning to be added a while ago: format-table should at least warn when it doesn't display properties #7871
Looks like out-gridview has similar struggles: Not all properties displayed
This appears to be one of PowerShell's weird quirks. I am not sure why it happens, but if someone else does I will happily edit my answer to include it.
You will need to specifically tell it to write the output to the console. There are a couple ways of doing this.
The first, and probably messiest, would be to pass it as a parameter to Write-Host or Write-Output.
Write-Host (Get-CimInstance Win32_Processor | Select-Object -Property NumberOfCores)
Write-Host (Get-CimInstance Win32_Processor | Measure-Object -Property LoadPercentage -Sum | Select-Object Sum)
(Another way of doing this would be to assign them both to variables, and pass the variables to the cmdlet)
The second way would be to pipe it to Write-Host or Write-Output
Get-CimInstance Win32_Processor | Select-Object -Property NumberOfCores | Write-Host
Get-CimInstance Win32_Processor | Measure-Object -Property LoadPercentage -Sum | Select-Object Sum | Write-Host
And finally, the third (and in my opinion cleanest) way would be to pipe it to a code block that references the property directly
Get-CimInstance Win32_Processor | %{$_.NumberOfCores}
Get-CimInstance Win32_Processor | Measure-Object -Property LoadPercentage -Sum | %{$_.Sum}
Hope this helps!

Powershell -lt and -gt giving opposite of expected results [duplicate]

This question already has an answer here:
How can I store output from Format-Table for later use
(1 answer)
Closed 3 years ago.
In the below code, if I add a where-object, -lt & -gt are giving opposite of expected results.
I'm sure the reason is I'm stupid, but in what specific way am I messing up?
This part gives the expected results, where in my case, the single drive has %Free of 39.8
Get-WmiObject -Namespace root\cimv2 -Class win32_logicaldisk | where-object -Property drivetype -eq 3 |
format-table deviceid,
#{n='GB Capacity';e={$_.size/1gb}},
#{n='GB Free';e={$_.freespace/1gb}},
#{n='%Free';e={($_.freespace/$_.size)*100}}
But adding this
| where {$_.'%Free' -gt 10}
Results in no output. In fact
| where {$_.'%Free' -gt 0}
Produces no results. Instead I have to use
| where {$_.'%Free' -lt 0}
Powershell thinks %Free is a negative number, I guess?
The problem is that you are piping Format-Table to anything. You should never use that except to output to the screen. Using Format-Table outputs everything as a format object, not whatever was piped into it. Instead use Select-Object to get what you need.
Get-WmiObject -Namespace root\cimv2 -Class win32_logicaldisk | where-object -Property drivetype -eq 3 |
Select-Object deviceid,
#{n='GB Capacity';e={$_.size/1gb}},
#{n='GB Free';e={$_.freespace/1gb}},
#{n='%Free';e={($_.freespace/$_.size)*100}}

Match dates with different formats in Powershell

I am trying to match the boot up time with the file modified date to be within a couple minutes of each other.
I can get the data but don't know how to do the computations. Here is what I get:
PS E:\> $disk=gci -Attributes hidden E:\filename.ext | Select LastWriteTime
PS E:\> $disk
LastWriteTime
-------------
6/23/2014 12:55:48 PM
PS E:\> $boottime=Get-CimInstance -ClassName win32_operatingsystem | select last bootuptime
PS E:\> $boottime
lastbootuptime
--------------
6/23/2014 12:55:39 PM
What I am looking for in English is a way to say "If lastwritetime is within 5 minutes of lastboot time, then all is good. If not, all is bad"
How can I do that with powershell?
I would subtract the datetimes and use the resulting TimeSpan object. Also, note that using the -ExpandProperty is needed to get a bare value with select-object.
$disk=gci -Attributes hidden E:\filename.ext | Select -ExpandProperty LastWriteTime
$boottime=Get-CimInstance -ClassName win32_operatingsystem | select -ExpandProperty lastbootuptime
if(($disk-$bootTime).TotalMinutes -lt 5){
"all is good"
}
Well, those are both dates and times, that's for sure. The issue is that the Get-WMI is returning some crazy CimInstance object that is relatively useless. What you can do with that is feed it to Get-Date, and that produces a [DateTime] object, and that is very useful! Try this out:
$boottime=get-date (Get-CimInstance -ClassName win32_operatingsystem | select -expand lastbootuptime)
Then you can do something like:
IF($Disk.Lastwritetime -lt $boottime.addminutes(5)){"All is good!"} else {"There is a disturbance in the force!"}

format-table outputting blank?

What I'm trying to do is look at some processes and get a list of the user of these processes. ANd the following code worked fine for me.
get-wmiobject win32_process |
where{$_.name -like "*notepad*"}|sort {$_.CommandLine}|
select #{n="User";e={$_.getowner().user}},#{n="ProcessID";e={$_.ProcessID}},{$_.CommandLine} |ft -AutoSize |Out-String -Width 300 >> C:\ListUsers.txt
Somehow I wanted to split the $_.CommandLine string in the middle of way, and output some of the split arrary(see the following code for a better idea, although the code is wrong). But the updated code just output nothing into the text file. I think I must be using the select-object or fommat-object wrong, but i don't know how i can fix it.
get-wmiobject win32_process |
where{$_.name -like "*notepad*"}|sort {$_.CommandLine}|
%{
$split = $_.CommandLine.split("\")
select #{n="User";e={$_.getowner().user}},#{n="ProcessID";e={$_.ProcessID}},#{n="Ihub";e=$split[3]},#{n="version";e=$split[3]},#{n="version";e=$split[3]} |
ft -AutoSize |Out-String -Width 300 >> C:\ListUsers.txt
}
Can anyone advise? Thanks!!
You don't need to use select and then format-table. Format-table can create calculated properties too. Also, you forgot to wrap the $split[3] in a scriptblock. I removed the two "version" properties because they were identical to "Ihub".
Try this(untested):
get-wmiobject win32_process |
where{$_.name -like "*notepad*"}|sort {$_.CommandLine} |
Format-Table -Property #{n="User";e={$_.getowner().user}},#{n="ProcessID";e={$_.ProcessID}},#{n="Ihub";e={($_.CommandLine.split("\"))[3]}} -AutoSize |
Out-String -Width 300 >> ListUsers.txt