'How to Append Text to File (with Powershell)
I am new to PowerShell, and am having trouble getting it to "echo" some variables back to a generated '.ps1' file. The use case is that I am attempting to make a script generator for media conversion that will generate another re-useable script once you enter all the required information.
Here is a sample of the code:
# Initialize
$TRACK_COUNTER=0
$artist=Read-Host -Prompt "Artist/Band Name"
$language=Read-Host -Prompt "Language (use ISO 639-2 codes)"
$album_or_single=Read-Host -Prompt "Is this an Album/Sound-Track or a collection of Singles (0 for album/sound-track, 1 for single's)"
# Album
if ($album_or_single -eq 0) {
$album=Read-Host -Prompt "Name of the Album"
$date=Read-Host -Prompt "Enter the release date of the album (in YYYY-MM-DD format, or type nothing if unknown)"
$genre=Read-Host -Prompt "Genre of the Album"
$cover_art=Read-Host -Prompt "Select the audio/image file with the Cover Art (ex. MP3/M4A or JPG/PNG)"
Get-ChildItem "*.mp3"
$track_amount=Read-Host -Prompt "How many Tracks/Songs are in this Album"
New-Item "$album [Generated].ps1" -ItemType File
Write-Output -Path "$album [Generated].ps1" -Value 'This is a text test.'
}
# Singles
if ($album_or_single -eq 1) {
$track_amount=Read-Host -Prompt "How many seperate Single's are there"
}
Sorry for the bad formatting, and the (probably) improper use of brackets. The problem is that nothing ends up going into the generated file (it actually ends up generating). The last thing Powershell spits out is:
"-Path In Sound Mind [Generated].ps1 (example sound-track) -Value This is a text test."
I'm sure I'm doing something very wrong, sorry if its a really easy fix. Thanks! (Also I'm doing this on Linux)
Revised sample (thanks Theo):
# Initialize
$TRACK_COUNTER=0
$artist=Read-Host -Prompt "Artist/Band Name"
$language=Read-Host -Prompt "Language (use ISO 639-2 codes)"
$album_or_single=Read-Host -Prompt "Is this an Album/Sound-Track or a collection of Singles (0 for album/sound-track, 1 for single's)"
# Album
if ($album_or_single -eq 0) {
$album=Read-Host -Prompt "Name of the Album"
$date=Read-Host -Prompt "Enter the release date of the album (in YYYY-MM-DD format, or type nothing if unknown)"
$genre=Read-Host -Prompt "Genre of the Album"
$cover_art=Read-Host -Prompt "Select the audio/image file with the Cover Art (ex. MP3/M4A or JPG/PNG)"
Get-ChildItem "*.mp3"
$track_amount=Read-Host -Prompt "How many Tracks/Songs are in this Album"
New-Item "$album [Generated].ps1" -ItemType File
Add-Content -LiteralPath "$album [Generated].ps1" -Value 'This is a text test.'
}
# Singles
if ($album_or_single -eq 1) {
$track_amount=Read-Host -Prompt "How many seperate Single's are there"
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
