简体   繁体   中英

Force Install-Module to always install to user folders

I want to force PowerShell to always install Modules into user space. Weirdly, Microsoft have a facility to force installation to System folders requiring Admin with Install-Module <Name> -Scope AllUser but do not have a way to force installation to user folders.

$ModulePath = "$(Split-Path $Profile)\Modules"   # Want all modules here.
$ModulePathTest = foreach ($i in ($env:PSModulePath).split(";")) { if ($i -like $ModulePath) { $True } }   # $ModulePathTest will be $true if $ModulePath is on the $env:PSModulePath
if ($ModulePathTest -eq $null) { <do stuff> }

So I got this far and now stuck. If I am logged on Admin, I think it installs by default to C:\\Program Files and if as a normal user, it will go to $ModulePath .

• Is there a quick way to add $ModulePath to $env:PSModulePath (and permanently, so I think this means it must be pushed into the registry right? I do not see a PowerShell command that can permanently update an environment variable - if I am wrong, would be good to know)?

• Is there a way that someone can think of to make Install-Module always install into $ModulePath in the above (ie C:\\Users\\<Username>\\Documents\\WindowsPowerShell\\Modules )?

Why not simply set the preference globally.

About Parameters Default Values

Short description Describes how to set custom default values for cmdlet parameters and advanced functions.

Long description The $PSDefaultParameterValues preference variable lets you specify custom default values for any cmdlet or advanced function. Cmdlets and advanced functions use the custom default value unless you specify another value in the command.

Syntax The $PSDefaultParameterValues variable is a hash table that validates the format of keys as an object type of System.Management.Automation.DefaultParameterDictionary. The hash table contains Key/Value pairs. A Key is in the format CmdletName:ParameterName. A Value is the DefaultValue or ScriptBlock assigned to the key.

The syntax of the $PSDefaultParameterValues preference variable is as follows:

$PSDefaultParameterValues=@{"CmdletName:ParameterName"="DefaultValue"}
$PSDefaultParameterValues=@{ "CmdletName:ParameterName"={{ScriptBlock}} }
$PSDefaultParameterValues["Disabled"]=$True | $False

Using $PSDefaultParameterValues in PowerShell

Let's say we want to make sure that we do not accidently stop any processes using Stop-Process. We can force WhatIf to always be used with Stop-Process to better protect ourselves:

$PSDefaultParameterValues.Add("Stop-Process:WhatIf",$True)

So, for what you are after, ...

What's in your PowerShell $PSDefaultParameterValues Preference Variable?

$PSDefaultParameterValues = @{
    'Out-Default:OutVariable' = 'LastResult'
    'Out-File:Encoding' = 'utf8'
    'Export-Csv:NoTypeInformation' = $true
    'ConvertTo-Csv:NoTypeInformation' = $true
    'Receive-Job:Keep' = $true
    'Install-Module:Scope' = 'CurrentUser'
    'Install-Module:AllowClobber' = $true
    'Install-Module:Force' = $true
    'Install-Module:SkipPublisherCheck' = $true
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM