locked
Select Multiple Strings and Format Output RRS feed

  • Question

  • I've been struggling with what to me should be a fairly simple script. I need to iterate through a text file and find two strings.

    I am able to do that but I want to Format to a table (Columns).  Is there a way to make this happen? 

    Here is my code - this works to get me the Username and FullName lines out of the Source file and into a new text file, but I cannot seem to figure out how to get "UserName" and "FullName" as Columns

    Clear-Host
    
    set-location C:\Temp
    
    $File = 'C:\Temp\userlist.txt'
    
    $regexA = 'UserName'
    $regexB = 'FullName'
    
    
    Get-Content  $File | 
        where {( ($_ | Select-String -pattern ($regexA) -SimpleMatch) -or (  $_ | Select-String -pattern ($regexB) -SimpleMatch)  ) } | out-file C:\Temp\UserInfo.txt -Append -Verbose

    I tried adding in the PipeLine  Format-Table but no formatting is happening

    Clear-Host
    
    set-location C:\Temp
    
    $File = 'C:\Temp\userlist.txt'
    
    $regexA = 'UserName'
    $regexB = 'FullName'
    
    
    Get-Content  $File | 
        where {( ($_ | Select-String -pattern ($regexA) -SimpleMatch) -or (  $_ | Select-String -pattern ($regexB) -SimpleMatch)  ) } | Format-Table | out-file C:\Temp\UserInfo.txt -Append -Verbose

    Anyone have any input? 

    Thank you in Advance


    • Edited by Kensmith89 Tuesday, January 30, 2018 6:55 PM had extra items at the end by mistake
    Tuesday, January 30, 2018 6:54 PM

Answers

  • Can you go directly to the registry?  Something like:

    Get-ItemProperty hklm:\software\adobe | select-object username,fullname


    • Edited by JS2010 Tuesday, January 30, 2018 8:47 PM
    • Marked as answer by Kensmith89 Wednesday, January 31, 2018 12:50 AM
    Tuesday, January 30, 2018 8:47 PM

All replies

  • Here is how to filter lines in a file:

    $File = 'C:\Temp\userlist.txt'
    $regexA = 'UserName'
    $regexB = 'FullName'
    Get-Content  $File |
    	where { $_ -match "$regexA|$regexB" } |
    	out-file C:\Temp\UserInfo.txt -Append


    \_(ツ)_/

    Tuesday, January 30, 2018 7:14 PM
  • What does the input look like?  Usually format-table is for objects, but it's the default output anyway:

    [pscustomobject]@{username='js';fullname='Joe Smith'}
    
    username fullname
    -------- --------
    js       Joe Smith
    

    Tuesday, January 30, 2018 7:42 PM
  • The input (Source file) was taken from the Registry (registry dump)

    Example in the input is:

    "Username"="icgroup"
    "CreationDateTime"=hex:01,30,00,58,53,e5,e6,64,51,20,c6,45,50,d1,cd,8f,70,04,\
      f2,c3,3e,ae,f7,a9,2f,30,1f,f8,93,bd,5c,6b,63,c0,72,15,1f,7d,65,c8,5a,c5,c7,\
      b1,c0,34,ef,32,0f,58,aa
    "Enabled"="1"
    "Membership"="|1|"
    "FullName"="ICGroup"

    I need to Group Username and FullName together.  Was hoping to create Columns

    Example:

    "Username"="icgroup"       "FullName"="ICGroup"


    • Edited by Kensmith89 Tuesday, January 30, 2018 8:15 PM
    Tuesday, January 30, 2018 7:51 PM
  • You will have to retrieve the lines one at a time and create a custom object as suggested above.


    \_(ツ)_/

    Tuesday, January 30, 2018 7:54 PM
  • @ JRV - thanks your code is doing exactly what my code does (as far as the final output), but yours ran a heck of a lot faster :)

    @ JS2010, I'm not quite up to speed on Custom Objects, could you possibly assist with how you would update my script?

    Thank you both

    Tuesday, January 30, 2018 8:27 PM
  • You can search for blogs that will teach you what objects are and how to use and create them.

    I can also suggest that you should extract this directly from the registry and not use a file.


    \_(ツ)_/


    • Edited by jrv Tuesday, January 30, 2018 8:39 PM
    Tuesday, January 30, 2018 8:38 PM
  • Can you go directly to the registry?  Something like:

    Get-ItemProperty hklm:\software\adobe | select-object username,fullname


    • Edited by JS2010 Tuesday, January 30, 2018 8:47 PM
    • Marked as answer by Kensmith89 Wednesday, January 31, 2018 12:50 AM
    Tuesday, January 30, 2018 8:47 PM
  • Good thinking !!  Thank you.. That was much easier and a lot shorter code :)

    I still need to learn up on Custom Objects though :)

    Wednesday, January 31, 2018 12:50 AM