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

I'm not sure that there is a way do it without temporary files and why it need at last, maybe for tuning PowerShell host at start? This sample demonstrates how to launch PowerShell host inside CommandPrompt session and change it caption on clock. (Note: be sure that you have enough rights to execute PowerShell scripts - Get-ExecutionPolicy).

Batch, 37 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
36
37
@echo off
  setlocal
    rem looking for PowerShell code
    for /f "tokens=* delims=:" %%i in ('findstr "^::" "%~dpnx0"') do (
      rem and  store it at *.ps1 file
      1>>script.ps1 (echo.%%i)
    )
    rem executing PowerShell script
    powershell /nologo /noprofile /noexit /file script.ps1
    rem remove *.ps1 file after closing host
    del /f /q "%~dp0script.ps1"
  endlocal
exit /b

:code
::$code = {
::  $regex = '\d{2}:\d{2}\d{2}'
::
::  do {
::    $clock = Get-Date -for 'HH:mm:ss'
::    $title = [Console]::Title
::
::    if ($title -match $regex) {
::      $fresh = $title -replace $regex
::    }
::    else {
::      $fresh = "$clock $title"
::    }
::
::    [Console]::Title = $fresh
::    sleep -sec 1
::  } while ($true)
::}
::
::$posh = [PowerShell]::Create()
::$null = $posh.AddScript($code)
::$posh.BeginInvoke() | out-null
Created by greg zakharov on Thu, 1 Nov 2012 (MIT)
Batch recipes (42)
greg zakharov's recipes (59)

Required Modules

  • (none specified)

Other Information and Tasks