sintrode

The correct way to embed powershell in a batch script

Jun 11th, 2024 (edited)
10,779
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 0.79 KB | None | 0 0
  1. <# :
  2. @echo off
  3. setlocal
  4. set "POWERSHELL_BAT_ARGS=%*"
  5. if defined POWERSHELL_BAT_ARGS set "POWERSHELL_BAT_ARGS=%POWERSHELL_BAT_ARGS:"=\"%"
  6. endlocal & powershell -NoLogo -NoProfile -Command "$_ = $input; Invoke-Expression $( '$input = $_; $_ = \"\"; $args = @( &{ $args } %POWERSHELL_BAT_ARGS% );' + [String]::Join( [char]10, $( Get-Content \"%~f0\" ) ) )"
  7. goto :EOF
  8. #>
  9.  
  10. Set-Location -Path "C:\Users\Admin\Documents"
  11. $folders = 1..10 | foreach { New-Item -Name $_ -ItemType Directory }
  12.  
  13. foreach ($folder in $folders) {
  14.  
  15.     Set-Location $folder.FullName
  16.     $Subfolders = 1..10 | foreach { New-Item -Name $_ -ItemType Directory }
  17.  
  18.     foreach ($Subfolder in $Subfolders) {
  19.  
  20.         Set-Location $Subfolder.FullName
  21.         1..10 | foreach { New-Item -Name $_ -ItemType Directory }
  22.  
  23.     }
  24.  
  25. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment