David Brierton
David Brierton

Reputation: 7397

Powershell Using Get-Date in a string to print in a txt file

In powershell if I run Get_Date by itself it will give me the correct date. But in my script when I run

"$computer" + "_Already_Had_Software_" + "(Get-Date)" | Out-File -FilePath "\\server\Install\Office2010\RemoteInstallfile.txt" -Append

It does not show the date but just says

_Already_Had_Software_Get-Date

Instead of showing the actual date.

Will someone please tell me where I may be going wrong?

Upvotes: 1

Views: 180

Answers (2)

Eris
Eris

Reputation: 7638

You should be using "$(Get-Date)". Your string forgot the $ that tells powershell to execute it.

Alternates:

  1. "${computer}_Already_Had_Software_$(Get-Date)"
  2. "$computer" + "_Already_Had_Software_" + (Get-Date)
  3. ({0}_Already_Had_Software_{1} -f $computer, (Get-Date))
  4. [String]::Join('_', ($computer, 'Already_Had_Software', (Get-Date)))

Upvotes: 2

saftargholi
saftargholi

Reputation: 980

Use this :

$date = Get-Date -Format "yyyy-MM-dd --- hh-mm-ss-fff tt Zone K" | Out-String
"$computer" + "_Already_Had_Software_" + "$date" | Out-File -FilePath "\\server\Install\Office2010\RemoteInstallfile.txt" -Append

Upvotes: 1

Related Questions