2

I need to convert what looks like a pretty simple bash script to powershell but I'm pretty new to both.

The original script is:

alias cptimestamp="date +"%Y%m%d%H%M" | clip"

I've gotten this far but I'm not sure:

function cptimestamp {
 cptimestamp="date +"%Y%m%d%H%M" | clip"
}

I'm having a hard time figuring out what the 'clip' part does.

4
  • | clip copies the output to the Windows clipboard (with a trailing newline). The bigger question is: What are you trying to do? Get the current date and time (in some particular format) and copy it to the clipboard? Commented Dec 9, 2021 at 19:28
  • 1
    Can you describe what your original script does or is intended to do? Commented Dec 9, 2021 at 19:32
  • @Bill_Stewart yes, and then it's being appended to a file name so that the dates are all in the same format Commented Dec 9, 2021 at 19:34
  • 1
    Why do you need to copy it to the clipboard? Commented Dec 9, 2021 at 19:36

2 Answers 2

6

The equivalent of date +"%Y%m%d%H%M" in PowerShell would be:

Get-Date -UFormat "%Y%m%d%H%M"

So your function should probably look like this:

function cptimestamp {
  Get-Date -UFormat "%Y%m%d%H%M" |Set-ClipBoard
}
Sign up to request clarification or add additional context in comments.

Comments

2

If the purpose of your PowerShell function is to copy the current date and time to the Windows clipboard, I would probably use this:

function cptimestamp {
  [Windows.Forms.Clipboard]::SetText((Get-Date -Uformat "%Y%m%d%H%M"))
}

Or alternatively:

function cptimestamp {
  [Windows.Forms.Clipboard]::SetText((Get-Date -Format "yyyyMMddhhmmss"))
}

Comments

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.