Simulating awk in or equivalent in Select-String

Hey all, can someone help this old Linux Admin to convert a command to powershell syntax? The part I need help with is the “awk” part at the end. I am just putting in the Linux command to hold the place. Basically I am getting the Name from nslookup and want to strip off “Name:” so that I am left with the FQDN. Example:
Name: mycomputer.mynet.org and then have only mycomputer.mynet.org

nslookup “192.168.10.1” | Select-String “Name:” | awk ‘{print $2}’

Eventually I will do a massive nslookup, strip out the FQDN, and use the resulting list of names to perform an admin function remotely on our network.

((nslookup 8.8.8.8 | Select-String "^name:") -split ":\s+")[1]

You can accomplish this using -split.

((nslookup "192.168.10.1" | Select-String "Name:") -split " +")[1]

The above:

  1. Does the nslookup
  2. Pipes results to Select-String which uses RegEx to find the string containing “Name:”
  3. 1 and 2 are enclosed in parenthesis so that they process before the -split operator takes affect
  4. The -split operator uses RegEx to split the string into an array based off of 1 or more space characters. This results in an array with 2 elements.
  5. Then we put that in parenthesis so that 1-4 process before we get the value of the element in position 1.

Hope that makes since.

Bob, Curtis: Thanks. yes it makes perfect since. I failed to tell you I was doing my basic testing at the PS command line so I am getting an “Expressions are only allowed as the first element of a pipeline.” I’ll try it in a basic script to see if that makes a difference.

Uhhh… Nevermind. Old habits are hard to break. I was actually, blindly, throwing in a | symbol.

To be clear a | symbol before the split syntax not the one before Select-String. :slight_smile: