4

How can I convert the below exported text to csv so that i can use it as objects in powershell. Eg:

where{$_.qlimit -eq 27}

Text:

  samid            qlimit    qused  
  Administrator    -1        0      
  Guest            -1        0      
  admin            27        8      
  krbtgt           -1        0      
  r                -1        0      
  admin2           44        0  
Martin Brandl
  • 56,134
  • 13
  • 133
  • 172
vinu
  • 81
  • 1
  • 1
  • 7

3 Answers3

6

Use the Get-Content cmdlet to load the file, replace two or more whitespaces with a comma and convert it to CSV using the ConvertFrom-Csv cmdlet:

$object = (Get-Content 'your_file').Trim() -replace '\s{2,}', ',' | ConvertFrom-Csv

If you now query your object:

$object | where qlimit -eq 27

You get the desired output:

samid qlimit qused
----- ------ -----
admin 27     8    
Martin Brandl
  • 56,134
  • 13
  • 133
  • 172
  • No i am getting the below error.. PS C:\Users\Administrator> $object = (Get-Content c:\temp.txt) -replace '\s{2,}', ',' | ConvertFrom-Csv ConvertFrom-Csv : Cannot process argument because the value of argument "name" is invalid. Change the value of the "name" argument and run the operation again. At line:1 char:77 + $object = (Get-Content c:\temp.txt) -replace '\s{2,}', ',' | ConvertFrom-Csv <<<< + CategoryInfo : InvalidArgument: (:) [ConvertFrom-Csv], PSArgumentException + FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.ConvertFromCsvCommand – vinu Jun 26 '16 at 07:39
  • I cannot reproduce this. can you upload the text file? – Martin Brandl Jun 26 '16 at 07:54
  • Martin, It worked .. thanks..please see my following code: $object = $users -replace '\s{3,}', ',' | ConvertFrom-Csv $object | where {$_.samid -eq "r"} $object where $_.qlimit -eq 44 – vinu Jun 26 '16 at 08:21
  • Fine, but you should use `.Trim()` (see my updated answer). Also, please consider to accepting the answer. – Martin Brandl Jun 26 '16 at 08:22
  • where should i use trim? – vinu Jun 26 '16 at 08:23
  • Thank you for the great answer, it worked for me. I slightly modified this code to convert the rows from the table to object properties. Sample code: $header = "Property", "Value" $collection = $stringTable.Trim() -replace '\s{2,}', ',' | ConvertFrom-Csv -Header $header and now I can do this: $collection | ForEach-Object {Write-Host "Do something with $($_.Property) and $($_.Value)"} – Stoimen May 09 '19 at 12:06
2

You can easily convert your table to PowerShell objects using the ConvertFrom-SourceTable cmdlet from the PowerShell Gallery:

ConvertFrom-SourceTable '
  samid            qlimit    qused  
  Administrator    -1        0      
  Guest            -1        0      
  admin            27        8      
  krbtgt           -1        0      
  r                -1        0      
  admin2           44        0  
' | Where-Object {$_.qlimit -eq 27}

Result:

samid qlimit qused
----- ------ -----
admin 27     8
iRon
  • 20,463
  • 10
  • 53
  • 79
0

Another way with ConvertFrom-String (no headers in the file):

(Get-Content 'your_file').trim() | ConvertFrom-String -PropertyNames samid,qlimit,qused
js2010
  • 23,033
  • 6
  • 64
  • 66