Can I hook PowerShell to call a function each time I execute a command?Useful Command-line Commands on WindowsWhy does my PowerShell script hang when called in PSEXEC via a batch (.cmd) file?How to make PowerShell add a new line after executing a command?Windows Powershell requires me to press Enter after the command finishesOpen Elevated “Administrator:” cmd prompt instead of “cmd prompt (Running as Administrator)”How can I escape double quote character for use in findstr on the PowerShell command line?Run Elevated Powershell prompt from command-lineCan I force powershell to ignore mouse input?Format-List in Powershell-Function not workingHow to call msiexec from powershell with parameters
Python 3 - simple temperature program
Why didn't this character get a funeral at the end of Avengers: Endgame?
Feasibility of lava beings?
Why would a military not separate its forces into different branches?
Will 700 more planes a day fly because of the Heathrow expansion?
Start job from another SQL server instance
History of the kernel of a homomorphism?
What do I do if my advisor made a mistake?
Can I use a Cat5e cable with an RJ45 and Cat6 port?
If all diagonals are drawn in a regular polygon from a vertex, the angles formed in that vertex are equal
Voltage Balun 1:1
How do I calculate how many of an item I'll have in this inventory system?
Does XQuartz work on macOS Mojave?
Gerrymandering Puzzle - Rig the Election
My first c++ game (snake console game)
How does summation index shifting work?
GitLab account hacked and repo wiped
Is there precedent or are there procedures for a US president refusing to concede to an electoral defeat?
Why is my arithmetic with a long long int behaving this way?
Can there be a single technologically advanced nation, in a continent full of non-technologically advanced nations?
Copy previous line to current line from text file
How can Internet speed be 10 times slower without a router than when using the same connection with a router?
A factorization game
Where are the "shires" in the UK?
Can I hook PowerShell to call a function each time I execute a command?
Useful Command-line Commands on WindowsWhy does my PowerShell script hang when called in PSEXEC via a batch (.cmd) file?How to make PowerShell add a new line after executing a command?Windows Powershell requires me to press Enter after the command finishesOpen Elevated “Administrator:” cmd prompt instead of “cmd prompt (Running as Administrator)”How can I escape double quote character for use in findstr on the PowerShell command line?Run Elevated Powershell prompt from command-lineCan I force powershell to ignore mouse input?Format-List in Powershell-Function not workingHow to call msiexec from powershell with parameters
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I want to change the title of the PowerShell window to the command line of the currently executing process inside it, just like CMD.EXE does.
Can I do this in PowerShell?
Is there some function like prompt
which is called when I execute a command in PowerSHell?
windows powershell console
add a comment |
I want to change the title of the PowerShell window to the command line of the currently executing process inside it, just like CMD.EXE does.
Can I do this in PowerShell?
Is there some function like prompt
which is called when I execute a command in PowerSHell?
windows powershell console
add a comment |
I want to change the title of the PowerShell window to the command line of the currently executing process inside it, just like CMD.EXE does.
Can I do this in PowerShell?
Is there some function like prompt
which is called when I execute a command in PowerSHell?
windows powershell console
I want to change the title of the PowerShell window to the command line of the currently executing process inside it, just like CMD.EXE does.
Can I do this in PowerShell?
Is there some function like prompt
which is called when I execute a command in PowerSHell?
windows powershell console
windows powershell console
edited Apr 26 at 4:26
Michael Hampton♦
176k27322653
176k27322653
asked Jul 30 '10 at 13:26
MehMeh
2331510
2331510
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Do you want it for a small select number of executables? Or all exes?
One hack for a select number of executables would to do
function cmd
$title = $host.UI.RawUI.WindowTitle
$host.UI.RawUI.WindowTitle = 'cmd.exe ' + ($args -join " ")
cmd.exe $args
$host.UI.RawUI.WindowTitle = $title
Then just run
cd c:
cmd /c dir /s
And see the title change
And for all the commands
Get-Command -CommandType Application | where $_.Name -match '.exe$' | %
$f = @'
function 0
$title = $host.UI.RawUI.WindowTitle
$host.UI.RawUI.WindowTitle = '0 ' + ($args -join " ")
0.exe $args
$host.UI.RawUI.WindowTitle = $title
'@ -f ($_ -replace '.exe', '')
Invoke-Expression $f
And then try ping 127.0.0.1
Its hacky, YMMV
Wow, your second solution is truly evil :)) Unfortunately it doesn't work for programs not in PATH, like scripts in the current folder
– Meh
Oct 1 '10 at 22:48
add a comment |
This is definitely possible, though you do have to code for it yourself. This TechNet article describes how to change the Title line of the executing window.
http://technet.microsoft.com/en-us/library/ee156814.aspx
That article only describes how to statically change the window title. I want a window title which adapts to what runs inside PowerShell. For example, when I type "PS> python.exe <ENTER>", I want the title bar to change to "Administrator: python.exe", just like in CMD.EXE. When python.exe terminates, the title bar should change back to "Administrator: Command Line"
– Meh
Jul 30 '10 at 19:13
add a comment |
Yes there is, this is my function for prompt, it put the last part of the actual path in the prompt. Also set the window title and when you runit as admin change the background and add a Admin: in the title.
$FirstRun=1
function prompt
$shortpath = split-path (Get-Location) -leaf;
$id = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$p = New-Object System.Security.Principal.WindowsPrincipal($id)
if
($p.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator))
$host.UI.RawUI.WindowTitle = "ADMIN:$shortpath"
if($FirstRun)$host.UI.RawUI.BackgroundColor = "Black"; cls; $global:FirstRun = 0;
else
$host.UI.RawUI.WindowTitle = $shortpath
$(if (test-path variable:/PSDebugContext)
'[DBG]: '
else '' ) + 'PS ' + $($shortpath) + $(if ($nestedpromptlevel -ge 1) '>>' ) + '> ';
Put that in your profile, remember that any profile is accesible directly with the profile variable:
$profile.CurrentUserCurrentHost, $profile.CurrentuserAllhost ....etc.
Thanks, but this doesn't do what I want. It only puts the name of the current path in the title bar, not the name of the executable of the current running command.
– Meh
Aug 10 '10 at 7:37
One thing you could try is to query for child processes of the current host (via WMI query perhaps?). That should give you the exe being run, which you can put in the title.
– scobi
Dec 4 '13 at 16:54
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "2"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fserverfault.com%2fquestions%2f165598%2fcan-i-hook-powershell-to-call-a-function-each-time-i-execute-a-command%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Do you want it for a small select number of executables? Or all exes?
One hack for a select number of executables would to do
function cmd
$title = $host.UI.RawUI.WindowTitle
$host.UI.RawUI.WindowTitle = 'cmd.exe ' + ($args -join " ")
cmd.exe $args
$host.UI.RawUI.WindowTitle = $title
Then just run
cd c:
cmd /c dir /s
And see the title change
And for all the commands
Get-Command -CommandType Application | where $_.Name -match '.exe$' | %
$f = @'
function 0
$title = $host.UI.RawUI.WindowTitle
$host.UI.RawUI.WindowTitle = '0 ' + ($args -join " ")
0.exe $args
$host.UI.RawUI.WindowTitle = $title
'@ -f ($_ -replace '.exe', '')
Invoke-Expression $f
And then try ping 127.0.0.1
Its hacky, YMMV
Wow, your second solution is truly evil :)) Unfortunately it doesn't work for programs not in PATH, like scripts in the current folder
– Meh
Oct 1 '10 at 22:48
add a comment |
Do you want it for a small select number of executables? Or all exes?
One hack for a select number of executables would to do
function cmd
$title = $host.UI.RawUI.WindowTitle
$host.UI.RawUI.WindowTitle = 'cmd.exe ' + ($args -join " ")
cmd.exe $args
$host.UI.RawUI.WindowTitle = $title
Then just run
cd c:
cmd /c dir /s
And see the title change
And for all the commands
Get-Command -CommandType Application | where $_.Name -match '.exe$' | %
$f = @'
function 0
$title = $host.UI.RawUI.WindowTitle
$host.UI.RawUI.WindowTitle = '0 ' + ($args -join " ")
0.exe $args
$host.UI.RawUI.WindowTitle = $title
'@ -f ($_ -replace '.exe', '')
Invoke-Expression $f
And then try ping 127.0.0.1
Its hacky, YMMV
Wow, your second solution is truly evil :)) Unfortunately it doesn't work for programs not in PATH, like scripts in the current folder
– Meh
Oct 1 '10 at 22:48
add a comment |
Do you want it for a small select number of executables? Or all exes?
One hack for a select number of executables would to do
function cmd
$title = $host.UI.RawUI.WindowTitle
$host.UI.RawUI.WindowTitle = 'cmd.exe ' + ($args -join " ")
cmd.exe $args
$host.UI.RawUI.WindowTitle = $title
Then just run
cd c:
cmd /c dir /s
And see the title change
And for all the commands
Get-Command -CommandType Application | where $_.Name -match '.exe$' | %
$f = @'
function 0
$title = $host.UI.RawUI.WindowTitle
$host.UI.RawUI.WindowTitle = '0 ' + ($args -join " ")
0.exe $args
$host.UI.RawUI.WindowTitle = $title
'@ -f ($_ -replace '.exe', '')
Invoke-Expression $f
And then try ping 127.0.0.1
Its hacky, YMMV
Do you want it for a small select number of executables? Or all exes?
One hack for a select number of executables would to do
function cmd
$title = $host.UI.RawUI.WindowTitle
$host.UI.RawUI.WindowTitle = 'cmd.exe ' + ($args -join " ")
cmd.exe $args
$host.UI.RawUI.WindowTitle = $title
Then just run
cd c:
cmd /c dir /s
And see the title change
And for all the commands
Get-Command -CommandType Application | where $_.Name -match '.exe$' | %
$f = @'
function 0
$title = $host.UI.RawUI.WindowTitle
$host.UI.RawUI.WindowTitle = '0 ' + ($args -join " ")
0.exe $args
$host.UI.RawUI.WindowTitle = $title
'@ -f ($_ -replace '.exe', '')
Invoke-Expression $f
And then try ping 127.0.0.1
Its hacky, YMMV
edited Apr 26 at 4:27
Michael Hampton♦
176k27322653
176k27322653
answered Oct 1 '10 at 5:41
mrwaimmrwaim
1537
1537
Wow, your second solution is truly evil :)) Unfortunately it doesn't work for programs not in PATH, like scripts in the current folder
– Meh
Oct 1 '10 at 22:48
add a comment |
Wow, your second solution is truly evil :)) Unfortunately it doesn't work for programs not in PATH, like scripts in the current folder
– Meh
Oct 1 '10 at 22:48
Wow, your second solution is truly evil :)) Unfortunately it doesn't work for programs not in PATH, like scripts in the current folder
– Meh
Oct 1 '10 at 22:48
Wow, your second solution is truly evil :)) Unfortunately it doesn't work for programs not in PATH, like scripts in the current folder
– Meh
Oct 1 '10 at 22:48
add a comment |
This is definitely possible, though you do have to code for it yourself. This TechNet article describes how to change the Title line of the executing window.
http://technet.microsoft.com/en-us/library/ee156814.aspx
That article only describes how to statically change the window title. I want a window title which adapts to what runs inside PowerShell. For example, when I type "PS> python.exe <ENTER>", I want the title bar to change to "Administrator: python.exe", just like in CMD.EXE. When python.exe terminates, the title bar should change back to "Administrator: Command Line"
– Meh
Jul 30 '10 at 19:13
add a comment |
This is definitely possible, though you do have to code for it yourself. This TechNet article describes how to change the Title line of the executing window.
http://technet.microsoft.com/en-us/library/ee156814.aspx
That article only describes how to statically change the window title. I want a window title which adapts to what runs inside PowerShell. For example, when I type "PS> python.exe <ENTER>", I want the title bar to change to "Administrator: python.exe", just like in CMD.EXE. When python.exe terminates, the title bar should change back to "Administrator: Command Line"
– Meh
Jul 30 '10 at 19:13
add a comment |
This is definitely possible, though you do have to code for it yourself. This TechNet article describes how to change the Title line of the executing window.
http://technet.microsoft.com/en-us/library/ee156814.aspx
This is definitely possible, though you do have to code for it yourself. This TechNet article describes how to change the Title line of the executing window.
http://technet.microsoft.com/en-us/library/ee156814.aspx
answered Jul 30 '10 at 15:24
sysadmin1138♦sysadmin1138
118k17146282
118k17146282
That article only describes how to statically change the window title. I want a window title which adapts to what runs inside PowerShell. For example, when I type "PS> python.exe <ENTER>", I want the title bar to change to "Administrator: python.exe", just like in CMD.EXE. When python.exe terminates, the title bar should change back to "Administrator: Command Line"
– Meh
Jul 30 '10 at 19:13
add a comment |
That article only describes how to statically change the window title. I want a window title which adapts to what runs inside PowerShell. For example, when I type "PS> python.exe <ENTER>", I want the title bar to change to "Administrator: python.exe", just like in CMD.EXE. When python.exe terminates, the title bar should change back to "Administrator: Command Line"
– Meh
Jul 30 '10 at 19:13
That article only describes how to statically change the window title. I want a window title which adapts to what runs inside PowerShell. For example, when I type "PS> python.exe <ENTER>", I want the title bar to change to "Administrator: python.exe", just like in CMD.EXE. When python.exe terminates, the title bar should change back to "Administrator: Command Line"
– Meh
Jul 30 '10 at 19:13
That article only describes how to statically change the window title. I want a window title which adapts to what runs inside PowerShell. For example, when I type "PS> python.exe <ENTER>", I want the title bar to change to "Administrator: python.exe", just like in CMD.EXE. When python.exe terminates, the title bar should change back to "Administrator: Command Line"
– Meh
Jul 30 '10 at 19:13
add a comment |
Yes there is, this is my function for prompt, it put the last part of the actual path in the prompt. Also set the window title and when you runit as admin change the background and add a Admin: in the title.
$FirstRun=1
function prompt
$shortpath = split-path (Get-Location) -leaf;
$id = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$p = New-Object System.Security.Principal.WindowsPrincipal($id)
if
($p.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator))
$host.UI.RawUI.WindowTitle = "ADMIN:$shortpath"
if($FirstRun)$host.UI.RawUI.BackgroundColor = "Black"; cls; $global:FirstRun = 0;
else
$host.UI.RawUI.WindowTitle = $shortpath
$(if (test-path variable:/PSDebugContext)
'[DBG]: '
else '' ) + 'PS ' + $($shortpath) + $(if ($nestedpromptlevel -ge 1) '>>' ) + '> ';
Put that in your profile, remember that any profile is accesible directly with the profile variable:
$profile.CurrentUserCurrentHost, $profile.CurrentuserAllhost ....etc.
Thanks, but this doesn't do what I want. It only puts the name of the current path in the title bar, not the name of the executable of the current running command.
– Meh
Aug 10 '10 at 7:37
One thing you could try is to query for child processes of the current host (via WMI query perhaps?). That should give you the exe being run, which you can put in the title.
– scobi
Dec 4 '13 at 16:54
add a comment |
Yes there is, this is my function for prompt, it put the last part of the actual path in the prompt. Also set the window title and when you runit as admin change the background and add a Admin: in the title.
$FirstRun=1
function prompt
$shortpath = split-path (Get-Location) -leaf;
$id = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$p = New-Object System.Security.Principal.WindowsPrincipal($id)
if
($p.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator))
$host.UI.RawUI.WindowTitle = "ADMIN:$shortpath"
if($FirstRun)$host.UI.RawUI.BackgroundColor = "Black"; cls; $global:FirstRun = 0;
else
$host.UI.RawUI.WindowTitle = $shortpath
$(if (test-path variable:/PSDebugContext)
'[DBG]: '
else '' ) + 'PS ' + $($shortpath) + $(if ($nestedpromptlevel -ge 1) '>>' ) + '> ';
Put that in your profile, remember that any profile is accesible directly with the profile variable:
$profile.CurrentUserCurrentHost, $profile.CurrentuserAllhost ....etc.
Thanks, but this doesn't do what I want. It only puts the name of the current path in the title bar, not the name of the executable of the current running command.
– Meh
Aug 10 '10 at 7:37
One thing you could try is to query for child processes of the current host (via WMI query perhaps?). That should give you the exe being run, which you can put in the title.
– scobi
Dec 4 '13 at 16:54
add a comment |
Yes there is, this is my function for prompt, it put the last part of the actual path in the prompt. Also set the window title and when you runit as admin change the background and add a Admin: in the title.
$FirstRun=1
function prompt
$shortpath = split-path (Get-Location) -leaf;
$id = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$p = New-Object System.Security.Principal.WindowsPrincipal($id)
if
($p.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator))
$host.UI.RawUI.WindowTitle = "ADMIN:$shortpath"
if($FirstRun)$host.UI.RawUI.BackgroundColor = "Black"; cls; $global:FirstRun = 0;
else
$host.UI.RawUI.WindowTitle = $shortpath
$(if (test-path variable:/PSDebugContext)
'[DBG]: '
else '' ) + 'PS ' + $($shortpath) + $(if ($nestedpromptlevel -ge 1) '>>' ) + '> ';
Put that in your profile, remember that any profile is accesible directly with the profile variable:
$profile.CurrentUserCurrentHost, $profile.CurrentuserAllhost ....etc.
Yes there is, this is my function for prompt, it put the last part of the actual path in the prompt. Also set the window title and when you runit as admin change the background and add a Admin: in the title.
$FirstRun=1
function prompt
$shortpath = split-path (Get-Location) -leaf;
$id = [System.Security.Principal.WindowsIdentity]::GetCurrent()
$p = New-Object System.Security.Principal.WindowsPrincipal($id)
if
($p.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator))
$host.UI.RawUI.WindowTitle = "ADMIN:$shortpath"
if($FirstRun)$host.UI.RawUI.BackgroundColor = "Black"; cls; $global:FirstRun = 0;
else
$host.UI.RawUI.WindowTitle = $shortpath
$(if (test-path variable:/PSDebugContext)
'[DBG]: '
else '' ) + 'PS ' + $($shortpath) + $(if ($nestedpromptlevel -ge 1) '>>' ) + '> ';
Put that in your profile, remember that any profile is accesible directly with the profile variable:
$profile.CurrentUserCurrentHost, $profile.CurrentuserAllhost ....etc.
edited Aug 9 '10 at 23:01
answered Aug 9 '10 at 22:52
mjsrmjsr
1517
1517
Thanks, but this doesn't do what I want. It only puts the name of the current path in the title bar, not the name of the executable of the current running command.
– Meh
Aug 10 '10 at 7:37
One thing you could try is to query for child processes of the current host (via WMI query perhaps?). That should give you the exe being run, which you can put in the title.
– scobi
Dec 4 '13 at 16:54
add a comment |
Thanks, but this doesn't do what I want. It only puts the name of the current path in the title bar, not the name of the executable of the current running command.
– Meh
Aug 10 '10 at 7:37
One thing you could try is to query for child processes of the current host (via WMI query perhaps?). That should give you the exe being run, which you can put in the title.
– scobi
Dec 4 '13 at 16:54
Thanks, but this doesn't do what I want. It only puts the name of the current path in the title bar, not the name of the executable of the current running command.
– Meh
Aug 10 '10 at 7:37
Thanks, but this doesn't do what I want. It only puts the name of the current path in the title bar, not the name of the executable of the current running command.
– Meh
Aug 10 '10 at 7:37
One thing you could try is to query for child processes of the current host (via WMI query perhaps?). That should give you the exe being run, which you can put in the title.
– scobi
Dec 4 '13 at 16:54
One thing you could try is to query for child processes of the current host (via WMI query perhaps?). That should give you the exe being run, which you can put in the title.
– scobi
Dec 4 '13 at 16:54
add a comment |
Thanks for contributing an answer to Server Fault!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fserverfault.com%2fquestions%2f165598%2fcan-i-hook-powershell-to-call-a-function-each-time-i-execute-a-command%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown