How to Create JSON Files with Content Using PowerShell?

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:

  1. Open PowerShell ISE or your preferred PowerShell editor.
  2. 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"
   }
  1. Use the ConvertTo-Json cmdlet to convert the PowerShell object to a JSON-formatted string:
   $jsonString = $data | ConvertTo-Json
  1. Save the JSON string to a file using the Out-File cmdlet:
   $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.

Create JSON Files with Content Using PowerShell

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.

PowerShell Create JSON Files with Content

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:

  1. Open PowerShell ISE or your preferred PowerShell editor.
  2. 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.

  1. Use the Add-Content cmdlet 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:

PowerShell Create JSON Files with Content

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:

  1. Open PowerShell ISE or your preferred PowerShell editor.
  2. Define the JSON content as a string variable, just like in the previous method:
   $jsonContent = @'
   {
       "Name": "Michael Smith",
       "Age": 40,
       "City": "Chicago"
   }
   '@
  1. Use the Set-Content cmdlet 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-Json for converting PowerShell objects to JSON, and Add-Content or Set-Content for 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:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.