sintrode icon

The correct way to embed powershell in a batch script

sintrode | PRO | 03/29/26 10:49:57 PM UTC (Edited) | 0 ⭐ | 10198 👁️ | Never ⏰ | []
Batch |

790 B

|

None

|

0 👍

/

0 👎

<# :
@echo off
setlocal
set "POWERSHELL_BAT_ARGS=%*"
if defined POWERSHELL_BAT_ARGS set "POWERSHELL_BAT_ARGS=%POWERSHELL_BAT_ARGS:"=\"%"
endlocal & powershell -NoLogo -NoProfile -Command "$_ = $input; Invoke-Expression $( '$input = $_; $_ = \"\"; $args = @( &{ $args } %POWERSHELL_BAT_ARGS% );' + [String]::Join( [char]10, $( Get-Content \"%~f0\" ) ) )"
goto :EOF
#>
 
Set-Location -Path "C:\Users\Admin\Documents"
$folders = 1..10 | foreach { New-Item -Name $_ -ItemType Directory }
 
foreach ($folder in $folders) {
 
    Set-Location $folder.FullName
    $Subfolders = 1..10 | foreach { New-Item -Name $_ -ItemType Directory }
 
    foreach ($Subfolder in $Subfolders) {
 
        Set-Location $Subfolder.FullName
        1..10 | foreach { New-Item -Name $_ -ItemType Directory }
 
    }
 
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎