IIIdefconIII
IIIdefconIII

Reputation: 363

powershell export variable to txt file

I need to extract a variable on a single line in a txt file like so;

Write-Host "Select RDS Collection to migrate"
$AllCollections.Keys | Sort-Object | ForEach-Object { Write-Verbose "[$_] - $($AllCollections.$_)" -Verbose }
Write-Output ''
$Collection = $AllCollections.$((Read-Host -Prompt 'Choose collection number') -as [int])

$oldrdscb = "RDSCB01N01"
$newrdscb = "RDSCB02N01"
$newrdscbsmb = "\\$newrdscb\C$\temp\migrationvariables.txt"

echo "$collection" = "$Collection" | Out-File -FilePath $newrdscbsmb -append -width 200

txt file \RDSCB02N01\c$\temp\migrationvariables.txt now containts:

RDS-FSLOGIXTEST
=
RDS-FSLOGIXTEST

this because it reads the first $collection and the gives the output of $collection variable, which makes sense. How can i get the result without the variable of the first $collection and send it as text instead of a variable so the end result in the txt would be:

$collection = TEST

The txt is emtpy before running this code. The Goal here is to read the variables on my new rds server to migrate theme.

Any tips? thank you!

Upvotes: 0

Views: 3042

Answers (1)

Josh Wright
Josh Wright

Reputation: 566

You just need to escape the dollar signs using the backtick (`). It is usually above the tab key on the keyboard.

$oldrdscb = "RDSCB01N01"
$newrdscb = "RDSCB02N01"
$newrdscbsmb = "\\$newrdscb\C$\temp\migrationvariables.txt"

echo "`$collectioname" = "`$Collection" | Out-File -FilePath $newrdscbsmb -append -width 200

Upvotes: 2

Related Questions