You will find requirements to work with JSON data as a system administrator or developer. JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write and easy for machines to parse and generate. In this tutorial, I will show you how to create JSON files with content using PowerShell, along with some examples.
Create JSON Files with Content Using PowerShell
Now, let me show you different methods to create JSON files with content using PowerShell.
Method 1: Using the ConvertTo-Json Cmdlet
PowerShell provides a built-in cmdlet called ConvertTo-Json that allows you to convert PowerShell objects into JSON-formatted strings. Here’s how you can use it to create a JSON file with content:
- Open PowerShell ISE or your preferred PowerShell editor.
- Create a PowerShell object that represents the data you want to store in the JSON file. For example:
$data = @{
Name = "John Doe"
Age = 30
City = "New York"
}- Use the
ConvertTo-Jsoncmdlet to convert the PowerShell object to a JSON-formatted string:
$jsonString = $data | ConvertTo-Json- Save the JSON string to a file using the
Out-Filecmdlet:
$jsonString | Out-File -FilePath "C:\Bijay\file.json"Replace "C:\Bijay\file.json" with the desired file path and name.
Here is the complete code for Windows OS.
$data = @{
Name = "John Doe"
Age = 30
City = "New York"
}
$jsonString = $data | ConvertTo-Json
$jsonString | Out-File -FilePath "C:\Bijay\file.json"For this example, I am using MaC OS; I have given the -FilePath of the Downloads folder.
$data = @{
Name = "John Doe"
Age = 30
City = "New York"
}
$jsonString = $data | ConvertTo-Json
$jsonString | Out-File -FilePath "/Users/bijay/Downloads/file.json"After I executed the code, you can see the file got created like the screenshot below.

That’s it! You have successfully created a JSON file with content using PowerShell and the ConvertTo-Json cmdlet.
You can see the content when you open the file.json file, like the screenshot below.

Check out How to Convert JSON to CSV in PowerShell?
Method 2: Using the Add-Content Cmdlet
Another approach to create a JSON file with content is by using the Add-Content cmdlet in PowerShell. This method is useful when you want to construct the JSON string manually. Here’s how you can do it:
- Open PowerShell ISE or your preferred PowerShell editor.
- Define the JSON content as a string variable. For example:
$jsonContent = @'
{
"Name": "Sarah Johnson",
"Age": 25,
"City": "Los Angeles"
}
'@Note the use of single quotes (') to preserve the double quotes (") inside the JSON string.
- Use the
Add-Contentcmdlet to write the JSON content to a file:
$jsonContent | Add-Content -Path "C:\Bijay\file.json"Replace "C:\Bijay\file.json" with the desired file path and name.
If you are using Windows OS, then you can use the below complete code.
$jsonContent = @'
{
"Name": "Sarah Johnson",
"Age": 25,
"City": "Los Angeles"
}
'@
$jsonContent | Add-Content -Path "C:\Bijay\file.json"If you want to run the PowerShell code in a MacOS, then you can use the below code.
$jsonContent = @'
{
"Name": "Sarah Johnson",
"Age": 25,
"City": "Los Angeles"
}
'@
$jsonContent | Add-Content -Path "/Users/bijay/Downloads/file.json"I executed the above PowerShell script in macos and you can see the exact output in the screenshot below:

You have now created a JSON file with the specified content using PowerShell and the Add-Content cmdlet.
Check out How to Convert String to JSON in PowerShell?
Method 3: Using the Set-Content Cmdlet
Similar to the Add-Content cmdlet, you can also use the Set-Content cmdlet to create a JSON file with content. The main difference is that Set-Content overwrites the file if it already exists, while Add-Content appends the content to the existing file. Here’s how you can use Set-Content:
- Open PowerShell ISE or your preferred PowerShell editor.
- Define the JSON content as a string variable, just like in the previous method:
$jsonContent = @'
{
"Name": "Michael Smith",
"Age": 40,
"City": "Chicago"
}
'@- Use the
Set-Contentcmdlet to write the JSON content to a file:
$jsonContent | Set-Content -Path "C:\Bijay\file.json"Replace "C:\Bijay\file.json" with the desired file path and name.
You have successfully created a JSON file with content using PowerShell and the Set-Content cmdlet.
Read How to Write JSON to File in PowerShell?
Best Practices and Tips
When creating JSON files with content using PowerShell, keep the following best practices and tips in mind:
- Use meaningful variable names to enhance the readability of your PowerShell scripts.
- Ensure that your JSON content follows the correct syntax and structure to avoid parsing errors.
- Choose the appropriate method based on your requirements. Use
ConvertTo-Jsonfor converting PowerShell objects to JSON, andAdd-ContentorSet-Contentfor manually constructing JSON strings. - Handle file paths and names carefully to avoid overwriting existing files unintentionally.
- Consider using try-catch blocks to handle potential errors gracefully.
Conclusion
In this tutorial, I have explained three methods to create JSON files with content using PowerShell. I have explained different methods such as using the ConvertTo-Json cmdlet to convert PowerShell objects to JSON, or manually constructing JSON strings with Add-Content or Set-Content, etc.
I hope this tutorial has been helpful in guiding you through the process of creating JSON files with content using PowerShell.
You may also like:
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.