PoshCode Logo PowerShell Code Repository

PowerShell script config by Dmitry Sotnikov 21 months ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1824"></script>download | new post

Stores script configuration on computer. This one is generic and would work with any PowerShell script/tool. A more PowerGUI specific one can be found at: http://dmitrysotnikov.wordpress.com/2010/05/07/storing-powergui-add-on-configuration

  1. #################################################
  2. # Sample code showing how to save/load PowerShell script
  3. # configuration to disk
  4. #
  5. # (c) Dmitry Sotnikov
  6. # http://dmitrysotnikov.wordpress.com/2010/05/07/storing-powergui-add-on-configuration
  7. #################################################
  8.  
  9. # Assign unique folder and config names
  10. # This would sore data in
  11. # C:\Users\username\AppData\Roaming\MyPowerGUIAddOn\MyAddOn.Config.xml
  12. $FolderName = "MyPowerGUIAddOn"
  13. $ConfigName = "MyAddOn.Config.xml"
  14.  
  15. # keep all add-on parameters in a map
  16. $parameters = @{}
  17.  
  18. # read parameters
  19.  
  20. if ( Test-Path -Path "$($env:AppData)\$FolderName\$ConfigName") {
  21.         $parameters = Import-Clixml -Path "$($env:AppData)\$FolderName\$ConfigName"
  22.          
  23.   # now you can use them, e.g.
  24.   $parameters['A']
  25.   $parameters['B']
  26. } else {
  27.   # Config does not exist yet - set defaults
  28.   $parameters['A'] = 5
  29.   $parameters['B'] = "Qwerty"
  30. }
  31.  
  32. # store parameters
  33.  
  34. if ( -not (Test-Path -Path "$($env:AppData)\$FolderName")) {
  35.   mkdir "$($env:AppData)\$FolderName"
  36. }
  37. $parameters | Export-Clixml -Path "$($env:AppData)\$FolderName\$ConfigName"

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me