Note that there are some explanatory texts on larger screens.

plurals
  1. POPowershell- Can't reference variable when making a file and setting its name with New-Item
    text
    copied!<p>Code:</p> <pre><code>$date = (Join-path C:\Users\&lt;username&gt;\Desktop\ (Get-Date).toString()) New-Item -Path c:\Users\&lt;username&gt;\Desktop -Name "" + $((Get-Date).toString()) + ".txt" -ItemType file $reader = [System.IO.File]::OpenText("C:\Users\&lt;username&gt;\Desktop\test.csv") try { for(;;) { $line = $reader.ReadLine() if ($line -eq $null) { break } # process the line Add-Content "C:\Users\&lt;username&gt;\Desktop\"($date.txt) -value $line $line } } finally { $reader.Close() } </code></pre> <p>I've tried it a dozen different ways, but I'm not sure how to do it. I'm trying to make a file with the date included as part of the name (for documentation, as I will be doing it several times regularly), and process each line of the file to be altered and put into the new file. Anyone know how to get this to work?</p> <p><strong>EDIT:</strong> Ok so I got it to read through, and it almost works. Unfortunately, i don't think I am calling the objects right. </p> <p>My code:</p> <pre><code>$fileName = '{0:yyyy-MM-dd HH-mm-ss}.html' -f (Get-Date) New-Item $fileName -itemtype file Add-content $fileName "&lt;html&gt;" Add-content $fileName "&lt;head&gt;" Add-content $fileName " &lt;title&gt;Call Logs&lt;/title&gt;" Add-content $fileName "&lt;head&gt;" Add-content $fileName "&lt;body&gt;" Add-content $fileName " &lt;div id='header'&gt;" Add-content $fileName " &lt;/div&gt;" Add-content $fileName " &lt;div id='table'&gt;" Add-content $fileName " &lt;/table&gt;" Add-content $fileName " &lt;tr&gt;" Add-content $fileName " &lt;th&gt;Date&lt;/th&gt;" Add-content $fileName " &lt;th&gt;Time&lt;/th&gt;" Add-content $fileName " &lt;th&gt;Call Type&lt;/th&gt;" Add-content $fileName " &lt;th&gt;Calling Number&lt;/th&gt;" Add-content $fileName " &lt;th&gt;Calling Extension&lt;/th&gt;" Add-content $fileName " &lt;th&gt;Calling Department&lt;/th&gt;" Add-content $fileName " &lt;th&gt;Called Number&lt;/th&gt;" Add-content $fileName " &lt;th&gt;Called Extension&lt;/th&gt;" Add-content $fileName " &lt;th&gt;Called Department&lt;/th&gt;" Add-content $fileName " &lt;th&gt;Call Connected&lt;/th&gt;" Add-content $fileName " &lt;th&gt;Duration&lt;/th&gt;" Add-content $fileName " &lt;th&gt;Queuing Time&lt;/th&gt;" Add-content $fileName " &lt;th&gt;Account Code&lt;/th&gt;" Add-content $fileName " &lt;th&gt;Carrier Code&lt;/th&gt;" Add-content $fileName " &lt;/tr&gt;" import-csv -path 'C:\users\&lt;username&gt;\Desktop\test.csv' | foreach-object{ $newRow = " &lt;tr&gt;&lt;td&gt;$_.Date&lt;/td&gt;&lt;td&gt;$_.Time&lt;/td&gt;&lt;td&gt;$_.'Call Type'&lt;/td&gt;&lt;td&gt;$_.'Calling Number'&lt;/td&gt;&lt;td&gt;$_.'Calling Extension'&lt;/td&gt;&lt;td&gt;$_.'Calling Department'&lt;/td&gt;&lt;td&gt;$_.'Called Number'&lt;/td&gt;&lt;td&gt;$_.'Called Extension'&lt;/td&gt;&lt;td&gt;$_.'Called Department'&lt;/td&gt;&lt;td&gt;$_.'Call Connected'&lt;/td&gt;&lt;td&gt;$_.Duration&lt;/td&gt;&lt;td&gt;$_.'Queuing Time'&lt;/td&gt;&lt;td&gt;$_.'Account Code'&lt;/td&gt;&lt;td&gt;$_.'Carrier Code'&lt;/td&gt;&lt;/tr&gt;" return $newRow } | Add-content $fileName </code></pre> <p>Now I get, with each call to $<em>.Whatever, everything. For example when I call $</em>.Date, I get </p> <pre><code>@{Date=7/1/2012; Time=16:43:06; Call Type=Terminating; Calling Number=XXXXXXXXXX; Calling Extension=; Calling Department=; Called Number=YYYYYYYYYY; Called Extension=XXXX; Called Department=Sales Engineers; Call Connected=No; Duration=0:00:11; Queuing Time=; Account Code=; Carrier Code=}.Date </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload