VSCode has become my and many other creators editor of choice. However, I find myself updating certain settings regularly. One of those settings is default file type for new files. An upside of changing the default file type is allowing the PowerShell extension to immediately parse your work. If you have not looked at the settings for VSCode I highly recommend you do so. You can browse to these settings by navigating to “File -> Preferences -> Settings”. Alternatively, you can open settings by using a keyboard shortcut -“Ctrl + ,”.
This will open all of the settings for your VSCode install. You can read all of the details about the Settings file here: https://code.visualstudio.com/docs/getstarted/settings
What is possibly the most important thing to know about VSCode Settings is that the settings are all controlled by a JSON file. The JSON control file is stored:
Depending on your platform, the user settings file is located here:
LinuxVSCode Website – https://code.visualstudio.com/docs/getstarted/settings
This of course means that once you get your settings exactly how you want them you can export and import your settings with PowerShell.
copy-item -Path "$ENV:AppData\Code\User\settings.json" -Destination C:\Users\jbenz001\Desktop\
You can then simply flip the two around to restore it. This is really useful if you use VSCode on multiple machines and you want to ensure you have the same settings everywhere.
Changing the default language – GUI Style
The designers of VSCode made finding anything in the settings file really easy. Once you have the settings file open, all you need to do is type in the search bar “Default Language” and you’ll see the default language option show up. By default the field is empty all you need to do is type in “powershell”
NOTE: JSON IS A CASE SENSITIVE LANGUAGE YOU MUST MAKE SURE THAT WHEN YOU TYPE IN POWERSHELL ITS ALL LOWER CASE.
Once you enter this and close the file it will auto save and all new files will be created with the .PS1 PowerShell extension by default and be handled by the PowerShell language parser.
Changing the default language – Using PowerShell AKA – Nerd Mode
Now if you remember we mentioned earlier on the VSCode file is nothing more than a JSON file. A JSON file that always lives in a specific location. We even hinted that we could replace the entire JSON file. However, if we can do that then certainly we can also just replace the line that controls the default language for new files right?
A little digging will reveal that:
"files.defaultLanguage": # The above is the filed that controls "files.defaultLanguage": "powershell", # The above is what we need to add to the Json file
Is the index the filed that controls what the default language is for new files. With this knowledge in mind we can of course make some changes. All we need to do is add the ‘”files.defaultLanguage”: “powershell”,’ to the file in the correct spot. Should be easy right?
First we need to use the code we used earlier to back up our settings file just in case something goes weird. Then we just need to add our above line to the file in the correct spot. We can do this in four lines of code.
#Retrieve the current settings.JSON content and convert it from JSON into a PowerShell object. $Obj = Get-Content -Path "$ENV:AppData\Code\User\settings.json" | ConvertFrom-Json #Add the member item and the property value that should be attached to it in this case Powershell $Obj | Add-Member -Name "files.defaultLanguage" -Value "powershell" -MemberType NoteProperty #Convert the content back to JSON $Content = ConvertTo-Json $Obj #Set the JSON and apply it over the top of the settings.JSON files Set-Content -Path "$ENV:AppData\Code\User\settings.json" -Value $Content
Simple! This will update the field and allow you to create all your new files as PowerShell files by default.