add-content with code as the content


I am working on some process automation and as a part of it I need to add a line to the end of a BAT script.

Here is what I have tried so far but is unsuccessful.

Add-Content D:\Batfile.bat -Value 'PowerShell.exe -Command ‘"& ‘%~dpn0.ps1’ $TNUM $BKID"’

Any help would be great.



You can. Your quoting is not correct here. There are some rules in using quotes inside quotes in PowerShell. See Get-Help About_Quoting_Rules

Thanks. That was a big help!

# Example adding line(s) to text/batch file

$FilePath = '.\MyBatchFile.bat'
$TNUM = 'some parameter data'
$BKID = 'another parameter data'
$LineToAdd = "PowerShell.exe -Command ""& %~dpn0.ps1 $TNUM $BKID"" "

$LineToAdd | Out-File $FilePath -Append  


# Example script to write another script (PS Profile in this example)
$ChildScript = @'
New-Item "$($env:SystemDrive)\scripts" -ItemType Directory -Force -EA 0 | Out-Null
cd "$($env:SystemDrive)\scripts" 
Set-PSRepository PSGallery -InstallationPolicy Trusted
Install-Module AzureRM,AZSBTools -Scope AllUsers -Force -WA 0 # -AllowClobber
New-Item (Split-Path $profile) -ItemType Directory -Force -EA 0 | Out-Null
$ChildScript | Out-File $profile -Force


PowerShell_ISE $profile # View child script in PS ISE (tab)


& $profile # Invoke child script