Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a powershell script that is hashing the MAC address, then posting it into a file and a clipboard.

$hash = [System.BitConverter]::ToString($md5.ComputeHash($utf8.GetBytes($MAC)))
$hash | clip
$hash | Out-File $Env:USERPROFILE\Desktop\this.txt 

The issue I am having is that a carriage return is being exported to the clipboard. I don't want that.

Is there a way of stopping that happening?

Additional info - When I paste the text that is on the clipboard into a hex editor I see 0D0A

The clipboard function would be fine if I could just loose that some how? Thanks

share|improve this question
    
By the way, that's actually two characters: CR (carriage return) and LF (line feed). In other words, a Windows new line. –  Bob Jul 19 '13 at 6:06

2 Answers 2

$Hash.TrimEnd() should clear any unicode whitespace at the end of the string. http://msdn.microsoft.com/en-us/library/system.string.trimend.aspx

share|improve this answer
    
That does not actually work. My theory is that clip itself inserts the newline. –  Bob Jul 19 '13 at 6:04

Unfortunately, it looks like clip itself appends the newline. So, let's do it the .NET way!

Here's a function that takes stdin and puts it in the clipboard:

function Out-ClipboardText
{
    param(  
        [Parameter(
            Position=0, 
            Mandatory=$true, 
            ValueFromPipeline=$true)
        ]
        [String]$text
    ) 

    process
    {
        powershell -sta -noprofile -command "add-type -an system.windows.forms; [System.Windows.Forms.Clipboard]::SetText('$text')"
    }
}

Adapted from http://brianreiter.org/2010/09/03/copy-and-paste-with-clipboard-from-powershell/ and http://stackoverflow.com/a/887406/1030702

Use it like so:

$hash | Out-ClipboardText

It uses the .NET clipboard function to set the text. Unfortunately, it's also a little slow and complicated because it needs to start a new PowerShell process in STA mode, as required for Clipboard (source).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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