PowerShell command/function

Darko 0 Reputation points
2023-02-21T18:47:05.33+00:00

Hi,

How can I create a function or a command that will write "Done!" at the end of finishing of every command I run in PowerShell?

Thank you for reading.

Windows for business Windows Server User experience PowerShell
{count} votes

4 answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2023-02-21T19:42:56.1066667+00:00

    I suppose you could use Write-Host "Done!" wherever you like. That's pretty tedious, and I don't think it's very helpful. If you're running commands individually from PowerShells' prompt the return of the prompt will signal the end of the command.

    0 comments No comments

  2. Limitless Technology 44,746 Reputation points
    2023-02-23T09:02:50.2633333+00:00

    Hello there,

    If Write-Output is the last command in the pipeline, the objects are displayed in the console.

    To create a function in Windows PowerShell, you begin with the Function keyword, followed by the name of the function. As a best practice, use the Windows PowerShell verb-noun combination when creating functions. Pick the verb from the standard list of Windows PowerShell verbs to make your functions easier to remember.

    This cmdlet is typically used in scripts to display strings and other objects on the console. One of the built-in aliases for Write-Output is echo and similar to other shells that use echo.

    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/write-output?view=powershell-7.3

    Hope this resolves your Query !!

    --If the reply is helpful, please Upvote and Accept it as an answer–

    0 comments No comments

  3. Sachith Lakmal 131 Reputation points
    2024-01-23T19:58:22.51+00:00

    Try this?

    function Invoke-CommandWithDone {
        param (
            [scriptblock]$ScriptBlock
        )
    
        & $ScriptBlock
    
       if ($?) {
            Write-Host "Done!"
        } else {
            Write-Host "Failed!"
        }
    }
    

    To use this function, you can call it and pass your command or script block as an argument:

    Invoke-CommandWithDone { ping 8.8.8.8 }
    

    lUser's image

    0 comments No comments

  4. Rich Matheisen 47,901 Reputation points
    2024-01-23T20:25:04.37+00:00

    This is a very old post! In addition to my earlier answer, here's another: The short answer is "no". A longer answer is "maybe". You can use the Set-BreakPoint cmdlet and list the cmdlets you're interested in. But this will cause a breakpoint at the start of the cmdlet, not at the end. An alternative is to insert Write-Verbose cmdlets in your script after every cmdlet. I think you may have to add [CmdletBinding()] at the start of your script. That way you can run your script and supply a "-Verbose" parameter when you run the script.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.