1

I have two files "name.txt" and "path.txt, contents for each file are below:

name.txt:

John
Mike
Paul

path.txt:

C:\directory\john.config
C:\directory\mike.config
C:\directory\paul.config

I want to print n'th line from each file together

Code:

$name = Get-Content "C:\path\name.txt"
$path = Get-Content "C:\path\path.txt"
foreach($var1 in $name){
foreach($var2 in $name){
Write-Host "$name - $path
}
}

Desired output:

John - C:\directory\john.config
Mike - C:\directory\mike.config
Paul - C:\directory\paul.config

My Output:

John - C:\directory\john.config C:\directory\mike.config 
C:\directory\paul.config
Mike - C:\directory\john.config C:\directory\mike.config 
C:\directory\paul.config
Paul - C:\directory\john.config C:\directory\mike.config 
C:\directory\paul.config

I want to get the desired output, can anyone please help me how I can get the desired output?

2
  • What have you written so far? Stackoverflow is not a code writing service. We are here to help you with problems in code that you are writing. Commented Feb 28, 2018 at 13:50
  • Let me edit the question again Commented Feb 28, 2018 at 13:51

1 Answer 1

2

This will work but without knowing a bit more about what you are trying to achieve it's difficult to tell whether it is a sensible solution or not.

$names = ( Get-Content -Path 'C:\temp\name.txt' )
$paths = ( Get-Content -Path 'C:\temp\path.txt' )
for($i = 0; $i -lt $names.Length; $i++) {
    Write-Host ( '{0} - {1}' -f $names[$i], $paths[$i] )
}
Sign up to request clarification or add additional context in comments.

1 Comment

If there is a need only for lines consists both parts it can be considered $i -lt [math]::min($names.Length, $paths.Length) as loop condition.

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.