3

I have a really simple PowerShell function that I use to make notes to myself:

New-Alias Note CreateNote    
function CreateNote ( [string]$note )
{
    $message = "`n" + $note
    Add-Content C:\notes.txt $message 
    Write-Host "Saved note."
}

This works great as long as I call it with a quoted string:

PS > Note "This is a note to myself."

I'd really like to be able to omit the quotes, similar to how Write-Host works:

PS > Write-Host This is a note to myself.
This is a note to myself.

It seems doable if I handle the arguments as an array and concatenate them before appending them to the text file. Is there a better way to do this?

1 Answer 1

5

You could use $args to accomplish this:

function CreateNote
{
    $message = "`n" + $args
    Add-Content D:\notes.txt $message 
    Write-Host "Saved note."
}

CreateNote this is a test
Sign up to request clarification or add additional context in comments.

1 Comment

Nice--worked like a charm. Stack Overflow won't let me mark this as the answer for five more minutes, though. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.