locked
Variable to Set-Content with variable path RRS feed

  • Question

  • I am trying to do the following

    From:

    Set-Content -path "F:\Powershell-Scripts\Policies\reports\data.html"

    to:

    $Logtime = Get-Date -Format "MM-dd-yyyy_hh-mm-ss"

    Set-Content -path "F:\Powershell-Scripts\Policies\reports\data" + $LogTime  + ".html"

    Error

    Set-Content : A positional parameter cannot be found that accepts argument '....

    Is there a way to get it working?

    Thanks


    • Edited by scubablue Wednesday, October 19, 2016 6:42 PM
    Wednesday, October 19, 2016 6:35 PM

Answers

  • Try putting parenthesis around it, so that it'll create the path, before it tries to use it.

    Set-Content -path ("F:\Powershell-Scripts\Policies\reports\data" + $LogTime  + ".html")

    Better, do this:

    Set-Content -path "F:\Powershell-Scripts\Policies\reports\data$LogTime.html"
    You can spot the newcomers, as they love to use the concatenation operator for their strings (the +). Don't forget that Set-Content is going to expect a parameter value for the Value parameter name.


    • Marked as answer by scubablue Wednesday, October 19, 2016 7:04 PM
    Wednesday, October 19, 2016 6:52 PM

All replies

  • Try putting parenthesis around it, so that it'll create the path, before it tries to use it.

    Set-Content -path ("F:\Powershell-Scripts\Policies\reports\data" + $LogTime  + ".html")

    Better, do this:

    Set-Content -path "F:\Powershell-Scripts\Policies\reports\data$LogTime.html"
    You can spot the newcomers, as they love to use the concatenation operator for their strings (the +). Don't forget that Set-Content is going to expect a parameter value for the Value parameter name.


    • Marked as answer by scubablue Wednesday, October 19, 2016 7:04 PM
    Wednesday, October 19, 2016 6:52 PM
  • you can either create a new string variable that contains the new path and file name you want, or just combine it into Set-Content -path "F:\Powershell-Scripts\Policies\reports\data$LogTime.html"

    Wednesday, October 19, 2016 6:54 PM
  • Thanks it works
    Wednesday, October 19, 2016 7:04 PM