如何在PowerShell中用变量替换字符串?

如何在PowerShell中用变量替换字符串?,powershell,Powershell,我有一段代码,我有点纠结于此: $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-Co

我有一段代码,我有点纠结于此:

$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--
Text.txt文件在所有文件夹中都是相同的,并且包含我希望更改的相同字符串(例如本例中的“Car”),但是新字符串应该来自我之前声明的变量和(或者使用数组更好?)


例如,$NumberOfProfiles=10,我创建了10个文件夹,其中的Text.txt完全相同,我需要将“Car”替换为folder1中的Variable1,folder2中的Variable2,依此类推

如果可以构造变量名,可以使用
Get variable
。举例说明:

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

Bike

不确定这是否有帮助,但可以使用param([string]$)调用传递到ps脚本中的参数