0

I have a batch/powershell script that I use to make a list of all my movies and tv series but have run into a small problem, I can't have spaces in my batch variables when passing them to the powershell script.

I have tried all kinds of combos of ' and " around the variables without any luck.

My batch variables

SETLOCAL enabledelayedexpansion
SET "listfolder=%CD%\ContentList\"

SET "listname1=TVSeries"
SET "RootFolder1=\\SERVER\Storage\TV Series\"
SET "Folder1Width=70"
SET ScanFolder1=TRUE

My batch code to run and pass variables to powershell

"%listfolder%Script\Powershell.exe" -executionpolicy remotesigned -File "%listfolder%Script\folder1list.ps1" %RootFolder1% %listname1% %Folder1Width%

My powershell script code to set the variables.

param([String]$RootFolder1, [String]$listname1, [int32]$Folder1Width)

This is the error I get if I have space in %RootFolder1% path, without spaces all three variables get passed to powershell just fine.

C:\Users\hflat\Desktop\Folder Content List Maker v4.5\ContentList\Script\folder1list.ps1 : Cannot process argument
transformation on parameter 'Folder1Width'. Cannot convert value "TV" to type "System.Int32". Error: "Input string was
not in a correct format."

I found the solution here How to pass in a string with spaces into PowerShell?

What did work was to use a cmd wrapper with /S to unwrap outer quotes and removing my own path to powershell.exe

cmd /S Powershell.exe -executionpolicy remotesigned -File "%listfolder%Script\folder1list.ps1" "%RootFolder1%" "%listname1%" "%Folder1Width%"
7
  • 1
    Is it possible to rewrite your batch script to powershell? Commented Apr 11, 2019 at 20:14
  • Did you try single or double quoting the arguments? '%RootFolder1%' '%listname1%' '%Folder1Width%' Commented Apr 11, 2019 at 20:23
  • I guess that could be done but would take some time. Commented Apr 11, 2019 at 20:24
  • cmd /S /C Powershell.exe -executionpolicy remotesigned -File "%listfolder%Script\folder1list.ps1" "%RootFolder1%" "%listname1%" "%Folder1Width%" This did work, apparently the path to powershell.exe itself made some problems. Commented Apr 11, 2019 at 20:26
  • 1
    Any environment variable value you want to pass as a single argument must be enclosed in "...". You don't need cmd /c to invoke powershell.exe. It's unclear if you still have a problem: if you found a solution, please share it as an answer, if you think it'll be of interest to future readers; if not, please consider deleting your question. Commented Apr 12, 2019 at 1:23

1 Answer 1

0

I found the solution here How to pass in a string with spaces into PowerShell?

What did work was to use a cmd wrapper with /S to unwrap outer quotes and removing my own path to powershell.exe

cmd /S Powershell.exe -executionpolicy remotesigned -File "%listfolder%Script\folder1list.ps1" "%RootFolder1%" "%listname1%" "%Folder1Width%"
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.