PowerShell: Getting Help on Get-Process -Property CPU - powershell

With PowerShell 3, I tried to get help on what properties are available for CPU; while using Get-Process. I just tried a shot in the dark, as below:
Help Get-Process -Property CPU
But, failed. Any help, please!

What are you looking for? Information about your processor? Get-Process list running processes(e.g. internet explorer) on your computer, not info about your processor-chips(CPU). Ex:
Get-Process
Output:
Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
284 25 7128 8748 103 1608 AppleMobileDeviceService
75 7 1136 1528 44 1588 armsvc
703 82 6612 7732 114 1,25 4212 AsusAudioCenter
Information about your processor can be found using:
Get-WmiObject Win32_Processor
Output:
Caption : Intel64 Family 6 Model 42 Stepping 7
DeviceID : CPU0
Manufacturer : GenuineIntel
MaxClockSpeed : 3400
Name : Intel(R) Core(TM) i7-2600 CPU # 3.40GHz
SocketDesignation : LGA1155
To get all properties about your CPU use Get-WmiObject Win32_Processor | fl *. To get a list of avaiable properties, use the Get-Member cmdlet to examine the object that Get-WmiObjectreturns:
Get-WmiObject Win32_Processor | Get-Member

Your shot in the dark missed. Also, since your description of what went wrong is nothing more than "But, failed.", I can only guess at what your problem might be. In order to better help you use help you need to help us by providing pertinent information about your problem such as error messages.
Firstly, Help (or the Get-Help cmdlet) does not have a -Property parameter. -Parameter might be what you looking for, however running Help Get-Process -Parameter CPU will reveal that the Get-Process cmdlet does not have a CPU parameter.
Secondly, Get-Process returns instances of the System.Diagnostics.Process class. The documentation or running Get-Process | Get-Member will show you what properties that class exposes. You can retrieve them by running something like...
Get-Process | Select-Object -Property (
'ProcessName',
'Id',
'ProcessorAffinity',
'UserProcessorTime',
'PrivilegedProcessorTime',
'TotalProcessorTime'
);
Finally, unlike previous versions PowerShell 3.0 does not install local help content. You need to run the Update-Help cmdlet to download and install help content. Alternatively, when running Get-Help you can pass the -Online parameter which will open the help content from MSDN in a web browser.

Related

Fetching values from Net Use command using powershell

I am trying to get network mapped drives using below commands.
Get-WmiObject -Class Win32_MappedLogicalDisk | %{$_.Name}
Get-WmiObject -Class Win32_MappedLogicalDisk | %{$_.ProviderName}
This works in some system however does not in other systems(may be powershell version issue) So I thought of using net use command. However, I am unable to fetch the values or not sure how to get the values displays when i type 'net use'
when I type net use I get status, Local, Remote and Network column. I tried to use the below command to get the field values.
net use | select local.
but I get blank or nothing
Used below command.
net use | select local.
Need to get Local and Remote values from net use command.
See this for parsing legacy console output ---
How to Convert Text Output of a Legacy Console Application to PowerShell Objects
Yet, along with what LotPings gave you already. Your query could be a duplicate of this ...
Equivalent of net use (to list computer's connections) in powershell?
... and it's accepted answer
# For the mapped logical drive you can use WMI class Win32_MappedLogicalDisk :
Get-WmiObject Win32_MappedLogicalDisk
# Here is another way with Win32_LogicalDisk :
PS C:\> Get-WmiObject -Query "Select * From Win32_LogicalDisk Where DriveType = 4"
DeviceID : V:
DriveType : 4
ProviderName : \\jpbdellf1\c$
FreeSpace :
Size :
VolumeName :
# Edited
# You are right, you can get what you need with Win32_NetworkConnection :
Get-WmiObject Win32_NetworkConnection
LocalName RemoteName ConnectionState Status
--------- ---------- --------------- ------
\\jpbasusf1\temp Connected OK
# On Seven or W2K8 be careful to call this with the same user that run the NET USE because it's a session information.
How about using get-psdrive (the root header actually matches the displayroot property)?
get-psdrive | where displayroot -like '\\*'
Name Used (GB) Free (GB) Provider Root
---- --------- --------- -------- ----
Y 91.84 7.82 FileSystem \\server....
Depending on the PowerShell versions available you might encounter similar problems with
Get-SmbMapping which wraps the CimClass: ROOT/Microsoft/Windows/SMB:MSFT_SmbMapping.
But has otherwise an output resembling net use.
To process the real net use output and convert to an object with properties,
you may use:
$SmbMapping = (net use) -like '* \\*' | ForEach-Object {
$Status,$Local,$Remote,$Null = $_ -split ' +',4
[PSCustomObject]#{
Status = $Status
Local = $Local
Remote = $Remote
}
}
This works at least in my German locale Win10.
(Not sure about different status messages in other locales.)

Why is Get-DnsClientServerAddress | select AddressFamily output not IPv4 and IPv6

When I type the cmdlet Get-DnsClientServerAddress I get all the interfaces my PC has like for example
InterfaceAlias Interface Address ServerAddresses
Index Family
-------------- --------- ------- ---------------
Ethernet 7 IPv4 {10.10.15.40, 10.10.25.44}
So when I type in Get-DnsClientServerAddress | where AddressFamily -Like "4" I would expect to see the Ethernet Adapter.
But for any reason it didn't show up. So I typed Get-DnsClientServerAddress | select AddressFamily and what I got was
AddressFamily
-------------
2
23
2
23
Can anyone explain this to me ?
As you found, the AddressFamily is categorised internally using a (not obvious) numbering scheme, where IPv4 addresses are type '2'. This comes from the underlying WMI type (MSFT_DNSClientServerAddress) and is not an issue with PowerShell.
The default display helps you out by translating this to IPv4, etc, but you can't filter on that as it's for display only. You can, however, still filter if you use the correct value:
Get-DnsClientServerAddress | Where-Object AddressFamily -Like 2
This formatting of data for display purposes happens all the time in PowerShell and is acheived through Format.ps1xml files. For example, compare the output of the Working Set values from Get-Process in table and list format:
PS C:\> Get-Process powershell
Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName
------- ------ ----- ----- ------ -- -- -----------
662 31 97928 110256 1.27 11452 2 powershell
PS C:\> Get-Process powershell | Format-List *
Handles : 705
VM : 2204040044544
WS : 113082368
PM : 100356096
NPM : 31512
The property (itself added by PowerShell for convenience) is called WS, but is shown as WS(K) in the table and the actual value is stored in bytes, but is displayed in KB, so some manipulation is going on for the default output.
Following from my comment, I would use Get-NetIPAddress instead.
Get-NetIPAddress -InterfaceAlias "Ethernet" | Select-Object FamilyAddress

How to cleanup $IE object, close current processes [duplicate]

There was a set of recently asked questions about doing something with Internet Explorer via PowerShell. All of them contain codes to launch IE from PowerShell as an object, via $ie=new-object -comobject InternetExplorer.Application. The problem is, the proper way of closing IE that consists of calling $ie.quit() does not work - first, if that IE would have happened to have more than a single open tab, IE doesn't quit as a whole and only the tab that corresponds to the COM object is closed, and second, should it have only one tab, the window gets closed but the processes remain.
PS > get-process iexplore
Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
352 31 7724 24968 142 0,58 3236 iexplore
228 24 22800 15384 156 0,19 3432 iexplore
I have tried to research the methods on how to close a process started via New-Object -ComObject, and have found this: How to get rid of a COMObject. The example of that COMObject is Excel.Application, which indeed behaves as intended - calling quit() makes window close, and executing [System.Runtime.Interopservices.Marshal]::ReleaseComObject($ex) if $ex is a created COMObject stops the Excel process. But this is not the case with Internet Explorer.
I have also found this question: How to get existing COM Object of a running IE which provides code to connect to IE via list of open windows, and works to an extent of IE launched from elsewhere, but if the COM object is created via PowerShell, this script is not able to completely stop IE's processes, if modified as such:
$shellapp = New-Object -ComObject "Shell.Application"
$ShellWindows = $shellapp.Windows()
for ($i = 0; $i -lt $ShellWindows.Count; $i++)
{
if ($ShellWindows.Item($i).FullName -like "*iexplore.exe")
{
$ie = $ShellWindows.Item($i)
$ie.quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($ie)
}
}
In case of IE launched outside of PowerShell, the processes are stopped, but in case of IE launched within PowerShell, two processes remain, and this code reports to have found no IE windows to reach COM objects, therefore IE processes are (yet) unable to be stopped.
So, how to reach the apparently orphaned windowless IE processes and gracefully stop them? I am aware of Get-Process iexplore | Stop-Process, but this will stop any and all IEs, not just those launched by the script, and if the script is run as administrator or SYSTEM on a, say, remote desktop server, everyone's IEs will be stopped.
Environment:
OS Windows 7 x64, PowerShell 4 (installed above PS version 2), IE11 version 11.0.9600.17691 (automatically updated). IE set to "Open home page" upon starting, so at least one tab is always open.
Simply calling the Quit() method should normally suffice for gracefully terminating Internet Explorer processes, regardless of whether they were created by running iexplore.exe or by instantiating a COM object in PowerShell.
Demonstration:
PS C:\> $env:PROCESSOR_ARCHITECTURE
AMD64
PS C:\> (Get-WmiObject -Class Win32_OperatingSystem).Caption
Microsoft Windows 8.1 Enterprise
PS C:\> Get-Process | ? { $_.ProcessName -eq 'iexplore' }
PS C:\> $ie = New-Object -COM 'InternetExplorer.Application'
PS C:\> Get-Process | ? { $_.ProcessName -eq 'iexplore' }
Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
352 20 4244 14164 176 0.05 3460 iexplore
407 32 6428 23316 182 0.23 5356 iexplore
PS C:\> $ie.Quit()
PS C:\> Get-Process | ? { $_.ProcessName -eq 'iexplore' }
PS C:\> _
If you have orphaned Internet Explorer processes to which you don't have a handle you can cycle through them like this:
(New-Object -COM 'Shell.Application').Windows() | Where-Object {
$_.Name -like '*Internet Explorer*'
} | ForEach-Object {
$_.Quit()
}
To be totally on the safe side you can release the COM object after calling Quit() and then wait for the garbage collector to clean up:
(New-Object -COM 'Shell.Application').Windows() | Where-Object {
$_.Name -like '*Internet Explorer*'
} | ForEach-Object {
$_.Quit()
[Runtime.Interopservices.Marshal]::ReleaseComObject($_)
}
[GC]::Collect()
[GC]::WaitForPendingFinalizers()
I've had similar problems with COM objects that wouldn't terminate using the quit() method. Interopservices.marshall also doesn't work a lot of times.
My workaround : I do a get-process to get a list of all procs before I call the com object and right after : this way I have the PID of my instance. After my script runs it kills the process using stop-process.
Not the best way to do this but at least it works
With a quick look around in the ComObject for IE, it seems that when it is created, it gives you a direct interface to the methods that make interacting with IE easier, for example Navigate() or ReadyState.
I did discover a property that seems to be what you are looking for and that would be Parent
Calling $IE.Parent.Quit() seemed to get rid of the PowerShell created instances.
$IE = New-Object -ComObject InternetExplorer.Application
Get-Process | Where-Object {$_.Name -Match "iex"}
Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
291 20 5464 14156 200 0.16 1320 iexplore
390 30 5804 20628 163 0.14 5704 iexplore
$IE.Parent.Quit()
(Get-Process | Where-Object {$_.Name -Match "iex"}).GetType()
You cannot call a method on a null-valued expression...
I tried an experiment with Powershell launching Excel via COM:
$x = New-Object -com Excel.Application
$x.Visible = $True
Start-Sleep 5 # make it stay visible for a little while
$x.Quit()
$x = 0 # Remove .NET's reference to com object
[GC]::collect() # run garbage collection
As soon as [GC]::collect() finished the process disappeared from taskmgr. This makes sense to me, because (in my understanding) a COM client is responsible for releasing references to the COM server. In this case .NET (via a Runtime Callable Wrapper) is the COM client.
The situation may be more complicated with IE, since there may be other tabs associated with a given IE process (and there's the frame merging that #Noseratio mentions), but at least this will get rid of the reference created by the PS script .
There's HKCU\Software\Microsoft\Internet Explorer\Main\FrameMerging registry key that prevents merging IE "frame" processes, explained here. I haven't tried it myself, but I think it might solve your problem if you set it before you instantiate the InternetExplorer.Application COM object.
If that doesn't help, try launching a new IE instance with the following command line, prior to creating the COM object (I haven't tried that, either):
iexplore.exe -noframemerging -private -embedding
There is a possible race condition before this IE instance becomes available as a COM server, so you may want to put some delay before you create an object.
This may be useful to you:
Get-Process | Where-Object {$_.Name -Match "iexplore"} | Stop-Process
This command closes all the Internet Explorer windows that are currently open/running:
Get-Process iexplore | Stop-Process

What is the -view parameter of Format-List?

Format-List apparently has a string parameter named "view", as can be seen here. What does it do, and how does it work? I cannot find any documentation beyond "The name of an alternate format or 'view.'"
The '-View' parameter on the various Format-* cmdlets allows you to get various different "views" or formattings of the data e.g.:
PS> Get-Process
Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
672 56 272684 220692 975 141.45 8480 powershell
692 34 47184 60156 234 23.73 17048 powershell
751 82 217624 162780 1047 157.73 13336 powershell_ise
versus
PS> Get-Process | Format-Table -View StartTime
StartTime.ToShortDateString(): 1/14/2013
ProcessName Id HandleCount WorkingSet
----------- -- ----------- ----------
powershell 8480 672 225988608
StartTime.ToShortDateString(): 2/6/2013
ProcessName Id HandleCount WorkingSet
----------- -- ----------- ----------
powershell 17048 624 92418048
StartTime.ToShortDateString(): 1/17/2013
ProcessName Id HandleCount WorkingSet
----------- -- ----------- ----------
powershell_ise 13336 771 166686720
As for determining which commands support alternate views, you can usually find such info in the docs. Here's an excerpt from the Get-Process help:
You can also use the built-in alternate views of the processes
available with Format-Table, such as "StartTime" and "Priority", and
you can design your own views. For more information, see
T:Microsoft.PowerShell.Commands.Format-Table.
The PowerShell Community Extensions also includes a command called Get-ViewDefinition that can get this info when the docs aren't available (or of much help in this regards.

Can I use PowerShell 1.0 to list processes along with their PIDs and Command Lines?

EDIT by OP: My question presupposed that PowerShell was the best tool for this job. There is a simpler way of achieving my goal. A friend just told me about: iisapp.vbs. It displays exactly the info I need without requiring PowerShell.
I'm working with dozens of ASP.NET websites running locally and when I want to debug a particular website named, for example, foo.site.com I go through the following steps:
Run Process Explorer (from SysInternals) and find which w3wp.exe was started with foo.site.com on its command line.
Note the Process ID (PID) of that w3wp.exe process.
In Visual Studio attach to that process ID.
Is there a way to write a PowerShell script that will print the PID and Command Line Arguments of every w3wp.exe process running on my computer?
When I run get-process w3wp I get:
> get-process w3wp
Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
688 28 44060 64576 226 2.75 196 w3wp
750 26 48328 68936 225 3.38 1640 w3wp
989 36 54596 83844 246 4.92 1660 w3wp
921 33 54344 80576 270 4.24 5624 w3wp
773 27 48808 72448 244 2.75 5992 w3wp
No Command Line information :(
Thanks!
EDIT: I am looking for the command line arguments that were passed to w3wp.
gwmi win32_process -filter "name='w3wp.exe'" | select name,processId,commandLine
It should do the trick. I find it weird that powershell doesn't provide command line information by default. Note : I've only tested it in powershell 2.0, but as it use wmi, it should work in 1.0.
EDIT : the final version used by Tim Stewart (to avoid display problem, see comment) :
gwmi win32_process -filter "name='powershell.exe'" | format-table -autosize name,processId,commandLine
My first instinct was to use get-process and look at the startinfo property:
get-process w3wp | select-object id, path, #{Name="Args";Expression = {$_.StartInfo.Arguments}}
Unfortunately, this doesn't work because $_.StartInfo.Argments is always null. WMI works, though.
get-wmiobject win32_process -filter "name='w3wp.exe'" | select-object processid, commandline
This should work:
get-process | format-table Id,Path