0

I have this code that I am struggling a little bit with:

$NumberOfProfiles = Read-Host "Enter a number of profiles"
$Directory = "C:\Users\Apple\Folder(1..HowMuchProfilesCreated\Text.txt"
Variable1 = "Plane"
Variable2 = "Yacht"
Variable100 = "Bike"
while ($NumberOfProfiles -gt 0) { 
(Get-Content $Directory) | %{$_.Replace("Car", "Don'tKnowWhatToPutHere")} | Set-Content $Directory 
$NumberOfProfiles--

The Text.txt file is same in all the folders and contains the same string I wish to change (e.g. "Car" in this case), but the new string should come from variables that I have declared before and(or may be it is better to use array?)

E.g. $NumberOfProfiles = 10 and I create 10 folders where are exactly the same Text.txt and I need the "Car" to be replaced with Variable1 in folder1, Variable2 in folder2 and so on.

1
  • not sure if this helps, but you can call paramters passed into the ps script with param( [string]$<variablename> ) Commented Jun 18, 2014 at 20:34

1 Answer 1

2

If you can construct the name of variable, you can use Get-Variable. An example to illustrate:

$Variable1 = "Bike"
$FolderID = 1
Get-Variable -Name "Variable$FolderID" -ValueOnly

Will result in

Bike
Sign up to request clarification or add additional context in comments.

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.