ajay k
ajay k

Reputation: 59

How to format powershell output as per requirement

I am running below PowerShell command to lookup a specific windows service.

Get-WMIObject Win32_Service | Where-Object {$_.name -like "HyS9FinancialManagementJavaServer_*" }|select name

which gives me expected output of

HyS9FinancialManagementJavaServer_epmsystem1.

My requirement is get only the string after _. Is there a way to achieve this in PowerShell?

Upvotes: 0

Views: 229

Answers (2)

mklement0
mklement0

Reputation: 437933

First, the obligatory advice:

  • The CIM cmdlets (e.g., Get-CimInstance) superseded the WMI cmdlets (e.g., Get-WmiObject) in PowerShell v3 (released in September 2012). Therefore, the WMI cmdlets should be avoided, not least because PowerShell (Core) v6+, where all future effort will go, doesn't even have them anymore. Note that WMI still underlies the CIM cmdlets, however. For more information, see this answer.

That said, given that you're seemingly targeting the local machine's services, using
Get-Service, which directly accepts wildcard-based name patterns, is simpler.

(Get-Service HyS9FinancialManagementJavaServer_*).Name -replace '^.+_'

Note the use of the regex-based -replace operator to strip everything up to and including the (last) _ (regex ^.+_) from (each) name.

Upvotes: 0

Harikrishnan
Harikrishnan

Reputation: 49

Try Name, Expression option while selecting

Get-WMIObject Win32_Service | Where-Object {$_.name -like "HyS9FinancialManagementJavaServer_*" }|Select-Object @{n="Name";e={(([String]($_.Name)).Split('_'))[1]}}

Upvotes: 1

Related Questions