0

I have created a batch file which contains the below line of code and when executed the batch file ,the new folder is not created.I don't want to create new .ps1 file and place the code in it.

START /W powershell -noexit $FolderPath= "C:\TestFolder"

#Check if Folder exists
If(!(Test-Path -Path $FolderPath))
{
New-Item -ItemType Directory -Path $FolderPath
Write-Host "New folder created successfully!" -f Green
}
  • 1
    That doesn't work. Your first command (start) is waiting for Powershell to finish before continuing with the next line - which will spit an error, as it isn't a valid *batch* command. – Stephan Apr 25 '23 at 12:33
  • There is a potential issue with your PowerShell script anyhow. It assumes that `New-Item -ItemType Directory -Path $FolderPath` will always succeed, because it is written to always write a successful message. If, in this case, the directory `TestFolder` cannot be created, because the end user does not have the required permissions to write to the root ```C:\```, your code would indicate differently. The task can however, regardless of the current answer, be performed from a batch file via PowerShell, and without creating a `.ps1` file. – Compo Apr 25 '23 at 14:56

3 Answers3

1

As Stephan has already commented, you can't run powershell code from within a batch script. There's literally no way to do it as the two systems use entirely different languages, have different security etc.

So your choices are to either :

1 - Use PowerShell, in which case you'll need to add that code to a .ps1 file and run it from there.

2 - Use Batch, in which case you'll need to change your code to use the batchscript method of doing what you need. So in the case of your example, you'd want to replace that code with the following batchscriptp code :

set FolderPath="c:\TestFolder\"
if not exist %FolderPath% (
  mkdir %FolderPath%
  echo "New folder created successfully!"
)

which will do the same thing (minus the green text).

Keith Langmead
  • 785
  • 1
  • 5
  • 16
  • I'd use `mkdir "%folderpath%" && echo New folder created successfully || echo failed to create folder` to make the word `successfully` valid. – Stephan Apr 26 '23 at 13:44
  • Of course you _can_ run PowerShell code from within a .BATch script. See [my answer](https://stackoverflow.com/a/76117268/778560)... – Aacini Apr 27 '23 at 06:24
  • Yeah you're right Aacini, can't believe I forgot you can call it AND tell it what to run essentially within a single line. – Keith Langmead Apr 27 '23 at 09:26
1

As per my earlier comment, here's a very quick example of what you posted, (plus the creation of an additional directory in order to show you the mechanism better), run directly from a batch file and without the need to create a .ps1 file:

@Echo Off
Call :PS "C:\TestFolder"
Call :PS "C:\AnotherTestFolder"
Exit /B

:PS
Start "" %SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -NoExit -Command "If(!(Test-Path -Path '%~1')){New-Item -ItemType Directory -Path '%~1'; Write-Host 'New folder created successfully!' -F Green}"

Please also remember my note about the success message being written to the host regardless of whether the directory was created.

Compo
  • 36,585
  • 5
  • 27
  • 39
1

You can write PowerShell code into a Batch .bat file following the rules stated at this answer. For example:

powershell  ^
   $FolderPath= 'C:\TestFolder';  ^
   If(!(Test-Path -Path $FolderPath)) {  ^
      New-Item -ItemType Directory -Path $FolderPath;  ^
      Write-Host 'New folder created successfully!' -f Green  ^
   }

You can review examples of large PowerShell sections into .BAT files at this thread, and even complete animated games like my VIBORAS.BAT (snakes) game in color...

Aacini
  • 65,180
  • 12
  • 72
  • 108