Home > database >  Capture substring at different positions from command's output in powershell
Capture substring at different positions from command's output in powershell

Time:01-18

I am running a command in windows terminal:

PS E:\> netsh wlan show profile name="wifi_profile" key=clear | findstr "Key Content"
    Key Content            : password1234

So here I am trying to get only password123 (wifi password) in output for any profile

Is there any method in powershell by which I can do it?

CodePudding user response:

In a simple cases like this, you can use the -replace operator to trim the unwanted part of the line:

(
  netsh wlan show profile name="wifi_profile" key=clear | findstr "Key Content"
) -replace '^. ?: '

Regex ^. ?: matches from the start (^) one or more ( ) characters (.) non-greedily (?) until the (first) : followed by a space, and - in the absence of a replacement operand passed to -replace - replaces it with the empty string, i.e. effectively removes it, thereby only returning the value part.

  •  Tags:  
  • Related