none
Powershell: Convert textfile to hashtable RRS feed

  • Question

  • Hi,

    I have this textfile c:\test.txt which contains items.
     F.e.
    Computer1
    Computer2
    Computer3

    Which command can read this file items and convert it so I have it like this $test = @(‘computer1’,’computer2’,’computer3’)?
    Reason is that I start with a textfile since it’s easy manipulated but want to keep the hashtable for later reference. Keeping the textfile is not that practical.

    so it would something like get-content c:\test.txt  then ConvertFrom-StringData (not sure if that's the correct way to go) $test but then I get
    Data line 'Computer (1)' is not in 'name=value format

    J.


    Jan Hoedt

    Tuesday, April 19, 2016 9:41 AM

Answers

  • This is what I needed:

    $Computers = gc c:\file.txt
    $ComputersArray = "@('" +  ($Computers -join "','") +  "')"


    Jan Hoedt



    • Edited by janhoedt Wednesday, April 20, 2016 3:05 PM update
    • Marked as answer by janhoedt Wednesday, April 20, 2016 3:11 PM
    Wednesday, April 20, 2016 2:59 PM

All replies

  • Not sure what purpose the hashtable would server you later than a text file.

    In the meantime, here is the conversion to hashtable.

    $servernames=Get-Content c:\test.txt
    foreach($servername in $servernames)
    {
    $test+=[ordered]@{
    		"$servername"=$null
    	}
    }
    echo $test


    Tuesday, April 19, 2016 12:51 PM
  • Hi,

    I have this textfile c:\test.txt which contains items.
     F.e.
    Computer1
    Computer2
    Computer3

    Which command can read this file items and convert it so I have it like this $test = @(‘computer1’,’computer2’,’computer3’)?
    Reason is that I start with a textfile since it’s easy manipulated but want to keep the hashtable for later reference. Keeping the textfile is not that practical.

    so it would something like get-content c:\test.txt  then ConvertFrom-StringData (not sure if that's the correct way to go) $test but then I get
    Data line 'Computer (1)' is not in 'name=value format

    J.


    Jan Hoedt

    What you are asking is how to get an array @() is an array and not a hash table.

    $test=Get-Content file.txt

    This will give you an array.


    \_(ツ)_/


    • Edited by jrv Tuesday, April 19, 2016 4:56 PM
    Tuesday, April 19, 2016 4:54 PM
  • Hash table indeed. But I didn't explain myself correctly. I don't want the variable itself because that's in memory.
    What I actually would like to do is keep the content of c:\test.txt in an array instead of a textfile. That way the content is in my script and not in a textfile wandering around. That's specifically important for my setup.
    So instead of a textfile with

    computer1
    computer2
    computer3

    I would like to have this content IN my script (and that's what I would like to extract via powershell from the textfile).

    $test = @(‘computer1’,’computer2’,’computer3’)


    Jan Hoedt

    Wednesday, April 20, 2016 2:28 PM
  • Get-Content already returns an array for a text file (if it contains more than one line), as jrv has pointed out already.

    If your script has a dependency on a text file, then document it and make sure the text file is there. In your script, you can use Test-Path to make sure the file exists.

    Other than that, I don't understand what you are asking. Get-Content is very efficient for short text files.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, April 20, 2016 2:34 PM
    Moderator

  • $test = @(‘computer1’,’computer2’,’computer3’)

    This is an array . Hashtable has a key value pair . And get-content does fulfill the requirement as per your comment "What I actually would like to do is keep the content of c:\test.txt in an array instead of a textfile. "


    Wednesday, April 20, 2016 2:44 PM
  • Copy the contents of you text file into the script:

    $mytest=@'
    comouter1
    compyter2
    computer3
    '@

    paste between the markers:

    $myarray=$mytext -split "`n"

    Now you have an array.


    \_(ツ)_/

    Wednesday, April 20, 2016 2:53 PM
  • This is what I needed:

    $Computers = gc c:\file.txt
    $ComputersArray = "@('" +  ($Computers -join "','") +  "')"


    Jan Hoedt



    • Edited by janhoedt Wednesday, April 20, 2016 3:05 PM update
    • Marked as answer by janhoedt Wednesday, April 20, 2016 3:11 PM
    Wednesday, April 20, 2016 2:59 PM
  • This is not a hash table. It is still an array. You have only concatenated hash table characters to the content.  Still not sure about the purpose of this. Your  original question was about converting text file to a hash table. May be we did not understand your requirement.


    Wednesday, April 20, 2016 3:04 PM
  • Correct, was wrong about hashtable, meant array. I explained as much as I could why I need it, can't give any further explanation.

    Jan Hoedt

    Wednesday, April 20, 2016 3:11 PM
  • This is what I needed:

    $Computers = gc c:\file.txt
    $ComputersArray = "@('" +  ($Computers -join "','") +  "')"


    Jan Hoedt



    This does nothing useful except build a weird string that looks like a hash table but isn't.

    It is not an answer to anything you think you have asked.


    \_(ツ)_/

    Wednesday, April 20, 2016 3:25 PM
  • It is making the text which I can copy past in my script. Sorry to have created the confusion, the question was  weird maybe but I do actually can use what I produced.

    Jan Hoedt

    Wednesday, April 20, 2016 3:51 PM
  • You don't need @(). This would also work:

    $computers = gc c:\file.txt
    $array = "'{0}'" -f ($computers -join "','")

    Different ways to type arrays if you want to maintain arrays in your script:

    # Option 1
    $array = 'computer1', 'computer2', 'computer3'
    $array
    
    # Option 2
    $array = @('computer1', 'computer2', 'computer3')
    $array
    
    # Option 3
    function ql{$args}
    $array = ql computer1 computer2 computer3
    $array


    Wednesday, April 20, 2016 4:19 PM