0

I'm currently trying to make ps1 script that creates another ps1 script via echo, add-content, clip and so forth.

What I'm trying to do is write a ps1 script that gets a folder, stores it in a variable ($VBS) and writes the new ps1 to a file in that folder to afterwards run it from that folder. Here's my code so far:

if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe -windowstyle hidden "-NoProfile -ExecutionPolicy RemoteSigned -File `"$PSCommandPath`"" -Verb RunAs; exit }
cd \ 
$VBS = gci -Recurse -Filter "Folder" -Directory -ErrorAction SilentlyContinue -path "C:\"
cd "$env:USERPROFILE\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"
echo $VBS | ForEach-Object { $_.FullName } > KickStart.ps1
cat KickStart.ps1 | set-clipboard   
echo '"if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe -windowstyle hidden "-NoProfile -ExecutionPolicy RemoteSigned -File `"$PSCommandPath`"" -Verb RunAs; exit }' > KickStart.ps1
add-content KickStart.ps1 "`ncd \"
add-content KickStart.ps1 "`ncd"
add-content KickStart.ps1 ' "'
add-content KickStart.ps1 | get-clipboard
add-content KickStart.ps1 '"'
add-content KickStart.ps1 '`nstart File.vbs' 
add-content KickStart.ps1 '`n cd \'
add-content KickStart.ps1 '`ncd "$env:USERPROFILE\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"'
add-content KickStart.ps1 'rm KickStart.ps1'

The final product (KickStart.ps1) should look like this:

if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe -windowstyle hidden "-NoProfile -ExecutionPolicy RemoteSigned -File `"$PSCommandPath`"" -Verb RunAs; exit }
cd \
cd "C:\PATH\TO\FOLDER"
start file.vbs
cd \
cd "$env:USERPROFILE\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"
rm KickStart.ps1

Instead my current scripts generates this:

"if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe -windowstyle hidden "-NoProfile -ExecutionPolicy RemoteSigned -File `"$PSCommandPath`"" -Verb RunAs; exit }

cd \

cd
 "
"
`nstart File.vbs
`n cd \
`ncd "$env:USERPROFILE\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"
rm KickStart.ps1

I seriously don't know what I'm doing wrong, I've put in almost 12 hours finding the solutions, help would be appreciated.

3
  • Did you ever try to understand your Add-Content lines? For example the first quote is literally included in your Add-Content lines, so naturally it would appear in your output. It also already includes a new line as per documentation. It's entirely unclear why you'd be calling Get-Clipboard. Commented Jan 6, 2020 at 12:10
  • Thing is i wanted to run gci for the folder of the vbs file, and declare it as a variable, while $VBS has the path info i can echo it into another file, aka "KickStart.ps1". But im not understading the add-content process, im trying to add the word(s) "cd" and even add the path info into the file with quotations around it, not sure how to string them. Instead though, the results dont come out right. Commented Jan 6, 2020 at 15:26
  • Read the documentation or check the answer provided below. Commented Jan 7, 2020 at 6:16

1 Answer 1

1

Although not entirely sure what you are trying to achieve, but looking at the desired output, why not build the content using a Here-String like this:

@'
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]"Administrator")) { 
    Start-Process powershell.exe -WindowStyle Hidden -NoProfile -ExecutionPolicy RemoteSigned -File "$PSCommandPath" -Verb RunAs
    exit 
}
Set-Location \ 
Set-Location "C:\PATH\TO\FOLDER"
Start-Process file.vbs
Set-Location \
Set-Location "$env:USERPROFILE\AppData\Roaming\Microsoft\Windows\Start-Process Menu\Programs\Startup"
Remove-Item KickStart.ps1
'@ | Set-Content -Path 'THE PATH FOR THE KickStart.ps1 FILE'

Result:

if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]"Administrator")) { 
    Start-Process powershell.exe -WindowStyle Hidden -NoProfile -ExecutionPolicy RemoteSigned -File "$PSCommandPath" -Verb RunAs
    exit 
}
Set-Location \ 
Set-Location "C:\PATH\TO\FOLDER"
Start-Process file.vbs
Set-Location \
Set-Location "$env:USERPROFILE\AppData\Roaming\Microsoft\Windows\Start-Process Menu\Programs\Startup"
Remove-Item KickStart.ps1
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.