title: How to create a folder and name it by using a computer name variable in Powershell?
body: <p>I'm new to Powershell. I would like the code to output results to a new folder and name it with a computer name variable. If the folder already exists, it should just output the result into that folder. </p>
  
  <p>What is the best way to do it?</p>
  
  <p>So far I have the following output code:</p>
  
  <pre><code>$dir = "C:\"
  
  $count = @{}
  $size = @{}
  $hostname = @{}
  gci $dir -recurse |%{
  [int]$count[$_.extension] += 1
  [int64]$size[$_.extension] += $_.length
  }
  $results = @()
  $count.keys | sort |% {
  $result = ""|select extension,count,size,hostname
  $result.extension = $_
  $result.count = $count[$_]
  $result.size = [math]::round($size[$_] /1Gb, 3)
  $result.hostname = $(get-content env:computername)
  $results += $result
  }
  $results | ft -auto
  
  $dirName = "C:\inetpub\wwwroot\${Env:ComputerName}"
  if (!(Test-Path $dirName)) { mkdir $dirName }
  
  
  $a = "&lt;style&gt;"
  $a = $a + "BODY{background-color:#A987CC;}"
  $a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;.center { margin:auto; width:70%; };}"
  $a = $a + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:#99CCFF}"
  $a = $a + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:PaleGoldenrod}"
  $a = $a + "&lt;/style&gt;"
  
  
  $results | sort-object -property size -Descending | select-object -first 30 | ConvertTo-Html extension,count,size, hostname "$a" -title "JUST TESTING :)" -body "I WAS HERE! :)" | 
  Set-Content C:\inetpub\wwwroot\${Env:ComputerName}\"$env:computername-$(get-date -f dd-MM-yyyy-hh-mm)".htm
  </code></pre>