<p>Has anyone worked out how to get PowerShell to use <code>app.config</code> files? I have a couple of .NET DLL's I'd like to use in one of my scripts but they expect their own config sections to be present in <code>app.config</code>/<code>web.config</code>.</p>

[{'answer_id': 18061, 'author': 'samjudson', 'author_id': 1908, 'author_profile': 'https://Stackoverflow.com/users/1908', 'pm_score': 3, 'selected': False, 'text': "<p>I'm guessing that the settings would have to be in powershell.exe.config in the powershell directory, but that seems to be a bad way of doing things.</p>\n\n<p>You can use ConfigurationManager.OpenMappedExeConfiguration to open a configuration file based on the executing DLL name, rather than the application exe, but this would obviously require changes to the DLLs.</p>\n"}, {'answer_id': 5625350, 'author': 'millerjs', 'author_id': 312103, 'author_profile': 'https://Stackoverflow.com/users/312103', 'pm_score': 6, 'selected': True, 'text': '<p>Cross-referencing with this thread, which helped me with the same question:\n<a href="https://stackoverflow.com/questions/2789920/subsonic-access-to-app-config-connection-strings-from-referenced-dll-in-powershel">Subsonic Access To App.Config Connection Strings From Referenced DLL in Powershell Script</a></p>\n\n<p>I added the following to my script, before invoking the DLL that needs config settings, where $configpath is the location of the file I want to load:</p>\n\n<pre><code>[appdomain]::CurrentDomain.SetData("APP_CONFIG_FILE", $configpath)\nAdd-Type -AssemblyName System.Configuration\n</code></pre>\n\n<p>See <a href="https://stackoverflow.com/questions/6150644/change-default-app-config-at-runtime/6151688#6151688">this</a> post to ensure the configuration file specified is applied to the running context. </p>\n'}, {'answer_id': 28241616, 'author': 'yzorg', 'author_id': 195755, 'author_profile': 'https://Stackoverflow.com/users/195755', 'pm_score': 2, 'selected': False, 'text': '<p><em>Attempting a new answer to an old question.</em></p>\n\n<p>I think the modern answer would be: don\'t do that.  PowerShell is a shell.  The normal way of passing information between parts of the shell are shell variables. For powershell that would look like:</p>\n\n<pre><code>$global:MyComponent_MySetting = \'12\'\n# i.e. \n$PSDefaultParameterValues\n$ErrorActionPreference\n</code></pre>\n\n<p>If settings is expected to be inherited across processes boundaries the convention is to use environment variables.  I extend this to settings that cross C# / PowerShell boundary.  A couple of examples:</p>\n\n<pre><code>$env:PATH\n$env:PSModulePath\n</code></pre>\n\n<p>If you think this is an anti-pattern for .NET you might want to reconsider.  This is the norm for PAAS hosted apps, and is going to be the new default for ASP.NET running on server-optimized CLR (ASP.NET v5).  </p>\n\n<p>See <a href="https://github.com/JabbR/JabbRv2/blob/dev/src/JabbR/Startup.cs#L21" rel="nofollow">https://github.com/JabbR/JabbRv2/blob/dev/src/JabbR/Startup.cs#L21</a> <br>\nNote: at time of writing I\'m linking to <code>.AddEnvironmentVariables()</code></p>\n\n<p>I\'ve revisited this question a few times, including asking it myself.  I wanted to put a stake in the ground to say PowerShell stuff doesn\'t work well with <code>&lt;appSettings&gt;</code>.  IMO it is much better to embrace the shell aspect of PS over the .NET aspect in this regards.  </p>\n\n<p>If you need complex configuration take a JSON string.  POSH v3+ has <a href="https://technet.microsoft.com/en-us/library/hh849898.aspx" rel="nofollow">ConvertFrom-JSON</a> built-in.  If everything in your process uses the same complex configuration put it in a .json file and point to that file from an environment variable.  </p>\n\n<p>If a single file doesn\'t suffice there are well established solutions like the <code>PATH</code> pattern, <strong>GIT .gitignore</strong> resolution, or <strong>ASP.NET web.config</strong> resolution (which I won\'t repeat here).</p>\n'}]
['https://Stackoverflow.com/questions/17960', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/419/']