Welcome, guest | Sign In | My Account | Store | Cart

Early I wrote about it, and now I want to go back and show you another way how to do it. This way more easly because all you need it just put your PowerShell script in the end of batch file.

Batch, 35 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
@echo off
  setlocal
    for /f "tokens=1 delims=:" %%i in ('^
      findstr /l /b /n /c:"exit /b" "%~dpnx0"') do set "n=%%i"
    more +%n% "%~dpnx0">>foo.ps1
    powershell /nologo /noprofile /noexit .\foo.ps1
  endlocal
exit /b
#PowerShell script begin
function Add-Clock {
  $code = {
    $reg = '\d{2}:\d{2}:\d{2}'
    do {
      $now = Get-Date -format 'HH:mm:ss'
      $old = [Console]::Title

      if ($old -match $pattern) {
        $new = $old -replace $pattern, $now
      }
      else {
        $new = "$now $old"
      }

      [Console]::Title = $new
      Start-Sleep -seconds 1
    } while ($true)
  }

  $ps = [PowerShell]::Create()
  [void]$ps.AddScript($code)
  $ps.BeginInvoke()
}

Add-Clock | Out-Null
Remove-Item .\foo.ps1

1 comment

Jedis 10 years ago  # | flag

Hi. This works, but the command prompt window never closes after the script ends.

Any ideas how to solve that?

Created by greg zakharov on Tue, 19 Mar 2013 (MIT)
Batch recipes (42)
greg zakharov's recipes (59)

Required Modules

  • (none specified)

Other Information and Tasks