PowerShell Solutions - Working with files

  • Comments posted to this topic are about the item PowerShell Solutions - Working with files

  • Hi Daniel

    This article is clearly written and very use full.


  • By "merge", do you mean "append"?

  • Good basics, thanks.

  • For the convert to html if you include the [ -notype ] at the end of your line of code the 2 PowerShell related columns will not show up in your report. See the example line below that I use to fetch the vlf's from a server using powershell except this is a conversion to a .csv file. I use the same technique for my html backup report.

    \output\VirtualLogFiles_$SQLServerName_$(get-date -f MM_dd_yyyy_HHmm)_$ServerStamp.csv -notype

  • Rather than:

    $date=(Get-Date -Format d).toString()


    I prefer using the format operator -f with a 4-digit year and fully padded days and months in YYYYMMDD order to make sorting easier. Modifying the formatting is very simple:

    $date = "{0:yyyyMMddHHmm}" -f (Get-Date)


    $date = "{0:yyyyMMddHHmmss}" -f (Get-Date)


    $date = "{0:yyyy-MM-dd-HH-mm-ss}" -f (Get-Date)


    $date = "{0:yyyy-MM-dd HH:mm:ss}" -f (Get-Date)

    2016-10-04 15:04:41

    Or in your example:

    $date = "{0:M-d-yyyy}" -f (Get-Date)


    With padded day and month:

    $date = "{0:MM-dd-yyyy}" -f (Get-Date)


    I typically use this mechanism to output a time-stamped file so I know when the file was created and on which computer. Note that grabbing seconds will most likely result in a unique filename unless the process producing this output can run in under 1000 ms.

    Out-File -FilePath ('c:\temp\{0}-{1:yyyyMMddHHmm}-a_meaningful_name.txt' -f $env:COMPUTERNAME,(Get-Date))


  • I don't see the "-notype" as an option for ContertTo-Html, and can't get it to work as suggested in another comment. But it would be nice to be rid of the tacked-on columns! Does anyone know of a way?

  • Took me a while to dig this one up use: -ExcludeProperty

    Notice I tucked it in prior to the convert to html, works well for my reports using html style output.

    | Select * -ExcludeProperty RowError, RowState, HasErrors, Table, ItemArray | ConvertTo-HTML -Head $Header | Out-File

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply