1

I have a windows task that executes a powershell command once a time at 1:15AM

Currently, they operation works using this command

Get-WinEvent -LogName Microsoft-Windows-PowerShell/Operational | Export-CSV C:\temp\Microsoft-Windows-PowerShell-Operational.csv

Problem is - the file gets written over each time it is executed.

I would like to use a filename of the date it was run.

Interactively, I have this code block and it works

$filenameFormat = "c:\temp\" + (Get-Date -Format "MM_dd_yyyy") + "_MWPSO.csv"
Get-WinEvent -LogName Microsoft-Windows-PowerShell/Operational | Export-CSV #filenameFormat

I've tried these commands

# Get-WinEvent -LogName Microsoft-Windows-PowerShell/Operational | Export-CSV "c:\temp\" + (Get-Date -Format "MM_dd_yyyy") + "_MWPSO.csv"

# $filenameFormat = "c:\temp\" + (Get-Date -Format "MM_dd_yyyy") + "_MWPSO.csv" ` Get-WinEvent -LogName Microsoft-Windows-PowerShell/Operational | Export-CSV $filenameFormat

# $filenameFormat = "c:\temp\" + (Get-Date -Format "MM_dd_yyyy") + "_MWPSO.csv"
# Get-WinEvent -LogName Microsoft-Windows-PowerShell/Operational | Export-CSV  $filenameFormat

Question : How do I use the newly created filename in a SINGLE line of code?

mklement0
  • 382,024
  • 64
  • 607
  • 775
pithhelmet
  • 2,222
  • 6
  • 35
  • 60

1 Answers1

1

How do I use the newly created filename in a SINGLE line of code?

You're already doing it in:

.. | Export-CSV "c:\temp\" + (Get-Date -Format "MM_dd_yyyy") + "_MWPSO.csv"

Just missing parentheses so the concatenation is evaluated first:

.. | Export-CSV ("c:\temp\" + (Get-Date -Format "MM_dd_yyyy") + "_MWPSO.csv")

Alternatively you can use $(..) for string interpolation:

.. | Export-Csv "C:\temp\$((Get-Date -Format 'MM_dd_yyyy'))_MWPSO.csv"
Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37