up vote 2 down vote favorite
1
share [g+] share [fb]

Some of the things I like in Bash and would love to know how to do in PowerShell:

  1. In Bash, I have history scrolling set up so that it only scrolls commands that begin with the same prefix as the current line. If I want to see my latest commit (e.g. to reuse part of the comment) I write 'git' and then .

  2. Related is of course the history search with Ctrl + R

  3. To find other things, I write:

    h | grep foo
    

    In PowerShell I use:

    h -c 1000 | where {$_.commandline.contains("foo")}
    

    (obviously I'm a newbie, there must be a shorter way)

  4. Things like:

    mv file.txt{,.bak}
    

    or

    mv file.txt !#$.bak
    
  5. Magic space (that expands !$ inline)

What are the alternatives in PowerShell?

link|improve this question

feedback

2 Answers

up vote 1 down vote accepted

For (3), you can write it as a function in your profile (e.g. %USERPROFILE%\My Documents\WindowsPowerShell\profile.ps1):

function hh ([string] $word) {
    Get-History -c 1000 | where {$_.commandline.contains($word)}
}

Then:

hh foo

But Powershell is best thought of as a scripting language than as an interactive shell, because the underlying console is still cmd.exe with all its limitations.

So it's F7 for interactive history, F3 to copy the previous command, F1 to copy a single character, F2 to copy the previous command up to a particular character, etc, etc.

link|improve this answer
feedback

1 - You can use F8 from the windows console to cycle through commands that match the beginning of the current line. This is a case sensitive match.

2 - You can use # to match previous commands. Using #<partial match><tab> is case insensitive and will match text at any position in the previous commands.

If you have the following command history:

# 1
$np = Start-Process notepad -PassThru
# 2
$np| get-process
# 3
$np| Stop-Process

Typing #pr then tab repeatedly will cycle through 1, 2 and 3.

Typing #st then tab repeatedly will cycle through 1 and 3.

Using only # will match all history.

# can also be used after entering part of a command. If your history is:

'notepad'
select *

You can type Get-Process #n<tab>| #s<tab> to get Get-Process 'notepad'| select *

3 - You can use Select-String. Create an alias for it to make it easy to use (PowerShell v3 added the alias sls). You could then do.

h| sls foo

4 - You can do something like:

gci *a.txt| ren -n {$_.Name + '.bak'}

5 - $$ matches the last token of the last command, but I don't know of a way to expand it inline.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.