title: Functions, variables in functions, and tighter coding practices
body: <p>I'm not totally new to powershell, it's just that my scripts have been getting more complex and some concepts elude me.  My scripts seem overly long because I use a lot of functions with repeatable pieces.  It seems like I could do variables somewhere in my functions but I really don't understand where to start (and I've been searching).</p>
  
  <p>This script formats and labels disks based on the number of disks added to a Windows virtual machine (2k8r2).  I use a lot of functions (and the script works...which is usually where I stop)... it's just that I'm just trying to improve my scripting abilities.  So the big question is how do I shorten this up?  Is there a better way to lay out and write my code?  Any advice would be appreciated.  Thanks</p>
  
  <pre><code>#Powershell script to pull info from diskpart and format disks
  #Date      : 09/15/2014
  
  Function DISKFORMAT
  {NEW-ITEM -Name detail.txt -ItemType file -force | OUT-NULL
  ADD-CONTENT -Path detail.txt "SELECT DISK $DISKID"
  ADD-CONTENT -Path detail.txt "CREATE PARTITION PRIMARY"
  ADD-CONTENT -Path detail.txt "FORMAT FS=NTFS QUICK"
  ADD-CONTENT -Path detail.txt "ASSIGN"
  $DETAIL=(DISKPART /S detail.txt)}
  
  Function SQLDIR1
  {New-Item -Path D:\MSSQL -ItemType directory
  New-Item -Path D:\MSSQL\DATA01 -ItemType directory
  New-Item -Path D:\MSSQL\TRANS01 -ItemType directory
  New-Item -Path D:\MSSQL\LOG01 -ItemType directory
  New-Item -Path D:\MSSQL\TEMPDB -ItemType directory}
  
  Function SQLDIR2
  {New-Item -Path D:\MSSQL -ItemType directory
  New-Item -Path D:\MSSQL\DATA01 -ItemType directory
  New-Item -Path D:\MSSQL\TRANS01 -ItemType directory
  New-Item -Path D:\MSSQL\LOG01 -ItemType directory
  New-Item -Path D:\MSSQL\TEMPDB -ItemType directory
  New-Item -Path D:\MSSQL\Backup -ItemType directory}
  
  Function SQLDATA01
  {NEW-ITEM -Name detail.txt -ItemType file -force | OUT-NULL
  ADD-CONTENT -Path detail.txt "SELECT DISK $DISKID"
  ADD-CONTENT -Path detail.txt "CREATE PARTITION PRIMARY"
  ADD-CONTENT -Path detail.txt "FORMAT FS=NTFS UNIT=64K QUICK"
  ADD-CONTENT -Path detail.txt "ASSIGN MOUNT=D:\MSSQL\DATA01"}
  
  Function SQLTRANS01
  {NEW-ITEM -Name detail.txt -ItemType file -force | OUT-NULL
  ADD-CONTENT -Path detail.txt "SELECT DISK $DISKID"
  ADD-CONTENT -Path detail.txt "CREATE PARTITION PRIMARY"
  ADD-CONTENT -Path detail.txt "FORMAT FS=NTFS UNIT=64K QUICK"
  ADD-CONTENT -Path detail.txt "ASSIGN MOUNT=D:\MSSQL\TRANS01"}
  
  Function SQLLOG01
  {NEW-ITEM -Name detail.txt -ItemType file -force | OUT-NULL
  ADD-CONTENT -Path detail.txt "SELECT DISK $DISKID"
  ADD-CONTENT -Path detail.txt "CREATE PARTITION PRIMARY"
  ADD-CONTENT -Path detail.txt "FORMAT FS=NTFS UNIT=64K QUICK"
  ADD-CONTENT -Path detail.txt "ASSIGN MOUNT=D:\MSSQL\LOG01"}
  
  Function SQLTEMPDB
  {NEW-ITEM -Name detail.txt -ItemType file -force | OUT-NULL
  ADD-CONTENT -Path detail.txt "SELECT DISK $DISKID"
  ADD-CONTENT -Path detail.txt "CREATE PARTITION PRIMARY"
  ADD-CONTENT -Path detail.txt "FORMAT FS=NTFS UNIT=64K QUICK"
  ADD-CONTENT -Path detail.txt "ASSIGN MOUNT=D:\MSSQL\TEMPDB"}
  
  Function SQLBACKUP
  {NEW-ITEM -Name detail.txt -ItemType file -force | OUT-NULL
  ADD-CONTENT -Path detail.txt "SELECT DISK $DISKID"
  ADD-CONTENT -Path detail.txt "CREATE PARTITION PRIMARY"
  ADD-CONTENT -Path detail.txt "FORMAT FS=NTFS UNIT=64K QUICK"
  ADD-CONTENT -Path detail.txt "ASSIGN MOUNT=D:\MSSQL\Backup"}
  
  Function LABEL1
  {$drive = Get-WmiObject -Class win32_volume -Filter "DriveLetter = 'C:'"
      Set-WmiInstance -input $drive -Arguments @{DriveLetter="C:"; Label="OS"}}
  
  Function LABEL2
  {$drive = Get-WmiObject -Class win32_volume -Filter "DriveLetter = 'C:'"
      Set-WmiInstance -input $drive -Arguments @{DriveLetter="C:"; Label="OS"}
  $drive = Get-WmiObject -Class win32_volume -Filter "DriveLetter = 'D:'"
      Set-WmiInstance -input $drive -Arguments @{DriveLetter="D:"; Label="Data"}}
  
  Function LABEL3
  {$drive = Get-WmiObject -Class win32_volume -Filter "DriveLetter = 'C:'"
      Set-WmiInstance -input $drive -Arguments @{DriveLetter="C:"; Label="OS"}
  $drive = Get-WmiObject -Class win32_volume -Filter "DriveLetter = 'D:'"
      Set-WmiInstance -input $drive -Arguments @{DriveLetter="D:"; Label="Apps"}
  $drive = Get-WmiObject -Class win32_volume -Filter "DriveLetter = 'E:'"
      Set-WmiInstance -input $drive -Arguments @{DriveLetter="E:"; Label="Data"}}
  
  Function LABEL4
  {$drive = Get-WmiObject -Class win32_volume -Filter "DriveLetter = 'C:'"
      Set-WmiInstance -input $drive -Arguments @{DriveLetter="C:"; Label="OS"}
  $drive = Get-WmiObject -Class win32_volume -Filter "DriveLetter = 'D:'"
      Set-WmiInstance -input $drive -Arguments @{DriveLetter="D:"; Label="Apps"}
  $drive = Get-WmiObject -Class win32_volume -Filter "DriveLetter = 'E:'"
      Set-WmiInstance -input $drive -Arguments @{DriveLetter="E:"; Label="Data"}
  $drive = Get-WmiObject -Class win32_volume -Filter "DriveLetter = 'F:'"
      Set-WmiInstance -input $drive -Arguments @{DriveLetter="F:"; Label="Data01"}}
  
  # List All disks
  NEW-ITEM –name listdisk.txt –itemtype file –force | OUT-NULL
  ADD-CONTENT –path listdisk.txt "LIST DISK"
  $LISTDISK=(DISKPART /S listdisk.txt)
  $TOTALDISK=($LISTDISK.Count)-7
  
  #If there's only one disk (OS DISK) Exit the script
  IF ($TOTALDISK -eq 1)
  {LABEL1}
  
  IF ($TOTALDISK -eq 2)
  {$DISKID=$LISTDISK[8].substring(7,5).trim()
  DISKFORMAT
  LABEL2}
  
  IF ($TOTALDISK -eq 3)
  {$DISKID=$LISTDISK[8].substring(7,5).trim()
  DISKFORMAT
  $DISKID=$LISTDISK[9].substring(7,5).trim()
  DISKFORMAT
  LABEL3}
  
  IF ($TOTALDISK -eq 4)
  {$DISKID=$LISTDISK[8].substring(7,5).trim()
  DISKFORMAT
  $DISKID=$LISTDISK[9].substring(7,5).trim()
  DISKFORMAT
  $DISKID=$LISTDISK[10].substring(7,5).trim()
  DISKFORMAT
  LABEL4}
  
  IF ($TOTALDISK -eq 5)
  {$DISKID=$LISTDISK[8].substring(7,5).trim()
  DISKFORMAT
  LABEL2
  SQLDIR1
  SQLDATA01
  SQLTRANS01
  SQLLOG01
  SQLTEMPDB}
  
  IF ($TOTALDISK -eq 6)
  {$DISKID=$LISTDISK[8].substring(7,5).trim()
  DISKFORMAT
  LABEL2
  SQLDIR2
  SQLDATA01
  SQLTRANS01
  SQLLOG01
  SQLTEMPDB
  SQLBACKUP}
  
  
  ELSE {EXIT}
  </code></pre>