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  
1
  • Now I think the last edit does not reflect the real input data : ) Commented Jun 26, 2016 at 8:34

3 Answers 3

7

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    
Sign up to request clarification or add additional context in comments.

6 Comments

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
I cannot reproduce this. can you upload the text file?
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
Fine, but you should use .Trim() (see my updated answer). Also, please consider to accepting the answer.
where should i use trim?
|
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

Comments

0

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

(Get-Content 'your_file').trim() | ConvertFrom-String -PropertyNames samid,qlimit,qused

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.