Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell retrieving a variable from a text file

Tags:

powershell

Is there a way to read a text file C:\test.txt and retrieve a particular value?

ie file looks like this:

serverName=serv8496 midasServer=serv8194 

I want to set the value of a variable in my script in some way from this file eg:

$MidasServer= (from file midasServer value) 

I will not know the line number where the reference is.

Any way to do this?

like image 553
user1662604 Avatar asked Sep 11 '12 10:09

user1662604


People also ask

How do I get data from a text file in PowerShell?

When you want to read the entire contents of a text file, the easiest way is to use the built-in Get-Content function. When you execute this command, the contents of this file will be displayed in your command prompt or the PowerShell ISE screen, depending on where you execute it.

How do you read a variable from a PowerShell script?

Therefore "VARIABLE=VALUE" and "VARIABLE = VALUE" in the file. txt returns the same. Set the scope of new variables to "Script". Variables created in the script scope are accessible only within the script file or module they are created in.

How do I find a word in a text file using PowerShell?

You can use Select-String similar to grep in UNIX or findstr.exe in Windows. Select-String is based on lines of text. By default, Select-String finds the first match in each line and, for each match, it displays the file name, line number, and all text in the line containing the match.


2 Answers

Yes, read the file, split each line and assign the split result to the Name and Value parameters:

Get-Content file.txt | Foreach-Object{    $var = $_.Split('=')    New-Variable -Name $var[0] -Value $var[1] } 
like image 104
Shay Levy Avatar answered Sep 20 '22 22:09

Shay Levy


If that is exactly how your file appears i.e. a list of key value pairs denoted with a equals sign then you should have a look at ConvertFrom-StringData which

converts a string that contains one or more key and value pairs into a hash table. Because each key/value pair must be on a separate line, here-strings are often used as the input format.

So if a text file contained just the data in your example you could do this to create a hashtable

$Path = "C:\temp\test.txt" $values = Get-Content $Path | Out-String | ConvertFrom-StringData $values.midasServer 

Where the $values.midasServer would have the value serv8194. No need to know where the properties are in respect to the file. Your input file can also have varying leading and trailing space around the equals sign which will give the exact same result.

Depending on your use case you can take that one step farther and create a custom object from that hashtable

New-Object -TypeName pscustomobject -Property $values 

If you have at least PowerShell v3 or higher you can simplify the process (assuming you want a custom psobject)

$values = [pscustomobject](Get-Content $Path -Raw | ConvertFrom-StringData) $values.midasServer 
like image 21
Matt Avatar answered Sep 18 '22 22:09

Matt