PoshCode Archive  Artifact [376848c993]

Artifact 376848c993d7dd9f27f34ee5e143aa070449c1dc47d542ef6edf8fd62704be5b:

  • File PowerShell-script-config.ps1 — part of check-in [959b25d9aa] at 2018-06-10 13:01:48 on branch trunk — 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 (user: unknown size: 1575)

# encoding: ascii
# api: powershell
# title: PowerShell script config
# description: 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
# version: 0.1
# type: script
# license: CC0
# x-poshcode-id: 1824
# x-archived: 2010-05-11T02:15:11
#
#
#################################################
# Sample code showing how to save/load PowerShell script
# configuration to disk
#
# (c) Dmitry Sotnikov
# http://dmitrysotnikov.wordpress.com/2010/05/07/storing-powergui-add-on-configuration
#################################################

# Assign unique folder and config names
# This would sore data in
# C:\Users\username\AppData\Roaming\MyPowerGUIAddOn\MyAddOn.Config.xml
$FolderName = "MyPowerGUIAddOn"
$ConfigName = "MyAddOn.Config.xml"

# keep all add-on parameters in a map
$parameters = @{}

# read parameters

if ( Test-Path -Path "$($env:AppData)\$FolderName\$ConfigName") {
	$parameters = Import-Clixml -Path "$($env:AppData)\$FolderName\$ConfigName"
	  
  # now you can use them, e.g.
  $parameters['A']
  $parameters['B']
} else {
  # Config does not exist yet - set defaults
  $parameters['A'] = 5
  $parameters['B'] = "Qwerty"
}

# store parameters

if ( -not (Test-Path -Path "$($env:AppData)\$FolderName")) {
  mkdir "$($env:AppData)\$FolderName"
}
$parameters | Export-Clixml -Path "$($env:AppData)\$FolderName\$ConfigName"