# Read 4 lines from a text file, assign data from each line to a variable.

• ### Question

• I created a web page asking for user input. my php exports the data to a text file, each input on a separate line.

1. Servername

2. End_date

3. Startup_time

How do i read the file and assign each of these lines to variables such as $server$end_date and $satrtup_time? Thank you for any help Monday, March 5, 2018 10:51 PM ### Answers • Also a good PowerShell technique to learn:$server_name, $end_date,$startup_time = get-content C:\path\to\your\file.txt | select -first 3

\_(ツ)_/

• Marked as answer by Wednesday, March 7, 2018 3:20 AM
• Edited by Wednesday, March 7, 2018 3:20 AM
Wednesday, March 7, 2018 3:19 AM
• How this works ...

PS D:\scripts> $a,$b,$c,$d,$rest_of_array = 1,2,3,4,5,6,7,8,9 PS D:\scripts>$a
1
PS D:\scripts> $b 2 PS D:\scripts>$c
3
PS D:\scripts> $rest_of_array 5 6 7 8 9 PS D:\scripts>  \_(ツ)_/ • Marked as answer by Wednesday, March 7, 2018 3:25 AM Wednesday, March 7, 2018 3:23 AM ### All replies • You have to post your script and any error messages. Please carefully review the following links to set your expectation for posting in technical forums. Script Center Script requests From a Bill Stewart summary of useful forum links: \_(ツ)_/ Monday, March 5, 2018 11:03 PM • $content = get-content -LiteralPath "C:\path\to\your\file.txt"
$server =$content[0]
$end_date =$content[1]
$startup_date =$content[2]

But really have a look at the links jrv posted.

• Edited by Tuesday, March 6, 2018 1:09 PM
• Proposed as answer by Wednesday, March 7, 2018 3:16 AM
Tuesday, March 6, 2018 9:16 AM
• that worked like a charm.  sorry still new to powershell and followed some other suggestions that did not work.

Appreciate your help and also the links to the other powershell resources. Excited to dig in deeper.

Ted

Thorsten Reichelt

Wednesday, March 7, 2018 3:08 AM
• Also a good PowerShell technique to learn:

$server_name,$end_date, $startup_time = get-content C:\path\to\your\file.txt | select -first 3 \_(ツ)_/ • Marked as answer by Wednesday, March 7, 2018 3:20 AM • Edited by Wednesday, March 7, 2018 3:20 AM Wednesday, March 7, 2018 3:19 AM • How this works ... PS D:\scripts>$a,$b,$c,$d,$rest_of_array = 1,2,3,4,5,6,7,8,9
PS D:\scripts> $a 1 PS D:\scripts>$b
2
PS D:\scripts> $c 3 PS D:\scripts>$rest_of_array
5
6
7
8
9
PS D:\scripts>


\_(ツ)_/

• Marked as answer by Wednesday, March 7, 2018 3:25 AM
Wednesday, March 7, 2018 3:23 AM
• it makes perfect sense now. i think looking at too may solutions using the split command ended up confusing me and sending me down the wrong path.

thank you again

Thorsten Reichelt

Wednesday, March 7, 2018 3:26 AM
• The path to Wisdom is all paths. You cannot play favorites. Just play.

\_(ツ)_/

Wednesday, March 7, 2018 3:48 AM