How do I write a powershell script to setup a developer environment on Windows?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How do I write a powershell script to setup a developer environment on Windows?



Stack Overflow,



I am working on a PowerShell script to setup a developer environment for my team. I want to have it so that a user can click a shortcut and have the necessary developer tools downloaded and installed for them. These installations should be global and on the PATH. I already have the shortcut that calls the script working. The script itself is what I am having issues with.



I am using chocolatey to try and install node on the developers machine's.



Here is the code I have written so far



devEnv.ps1


echo "Installing Chocolatey package manager, check version with choco"
$env: ChocolateyInstall="$Home/chocolatey"
iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
echo "Chocolatey installed. Installing NodeJS and NPM via Chocolatey."
Start-Process powershell.exe -argument "-NoExit -Command ./nodejs.ps1"
echo "Installation complete."
Write-Host -NoNewLine 'Press any key to continue...';
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');



I am able to install chocolatey successfully, however I am not able to install node. I created another script that I call on line 5 of devEnv.ps1



I was hoping that the new PowerShell instance would have access to the choco command. It does not. Below I have included the second script.



nodejs.ps1


Invoke-Expression -Command "choco install nodejs.install"



Error Returned


choco : The term 'choco' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check
the spelling of the name, or if a path was included, verify that the path
is correct and try again.
At line:1 char:1
+ choco install nodejs.install
+ ~~~~~
+ CategoryInfo : ObjectNotFound: (choco:String) ,
CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException





This may be obvious, but does your process have privileges to install chocolatey and create the machine environment variables (in this case editing $Env:PATH)?
– TheIncorrigible1
Aug 6 at 22:22


$Env:PATH





Last word of wisdom: Never use Invoke-Expression. The chocolatey installer instructs you to for a very specific reason, but it opens your scripts up to injection; ESPECIALLY when executing code from the Internet.
– TheIncorrigible1
Aug 6 at 22:24



Invoke-Expression




2 Answers
2



So I walked through your script and ran into some room for improvement:


$Env:ChocolateyInstall = "$HOMEchocolatey"

$chocoInstall = New-TemporaryFile
'Installing chocolatey package manager.'
'Check version with `choco.exe --version`.'
[Net.WebClient]::new().DownloadString(
'https://chocolatey.org/install.ps1'
) | Out-File -FilePath $chocoInstall.FullName -Encoding UTF8
. $chocoInstall.FullName

'Chocolatey installed. Installing NodeJS and NPM via Chocolatey.'
& "$Env:ProgramDatachocolateychoco.exe" install nodejs --yes --force

'Installation complete.'

'Press any key to continue...'
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')



This will address your issue of choco.exe not being found in the PATH environment variable by full-pathing the executable call. Additionally, it will address cases where npm may already be installed and auto-confirms the package.


choco.exe


PATH


npm





From parsing through this answer it seems like this avoids calling another script using start-process. Nice! That cleans up the script a great deal. Will have to try this. I will mark the answer as accepted if it works.
– BradleyGamiMarques
Aug 7 at 13:21



You may want to look into Boxstarter for this use case. Boxstarter will handle the reboots needed for certain tools and has additional options for configuring the working environment (configuring Explorer options as an example). It also handles this particular case where choco.exe is not found just after installing it (as the PATH has been updated but the Windows hasn't updated the current session with the change).



You'll find it at https://boxstarter.org






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS