How to Replace Text in Files with PowerShell

Code is developed on: Windows 10 Version 1703, Build 10.0.15063.138

In this short article I will show you how to replace a matched string in multiple files. In this use case I will search for Windows Media Player playlists on \\fileserver1\media\audio. Because we are implementing DFS Namespaces, the links inside the playlists needs to be changed to \\runas\dfs\media\audio.



  • Line 1: Collects the file information in the variable $wpls
  • Line 2: We start a foreach loop here. For every file object we execute the next action:
  • Line 3: For the file object we grab the contents, search for the string ‘fileserver1’ and replace this with ‘runas\dfs’. This will only change the values in the object. After we pipe this to Out-File to write it back to the original file.

Visual Studio Code Survival Guide for PowerShell Coders

In this series I will share information on how to use Microsoft’s Visual Studio Code for PowerShell coding. The article contains tips/tricks to be better able to use VS Code. It’s a personal log that I keep in my learning process for VS Code. Hopefully it helps you too!

Useful Links

Change the Default Language Mode to PowerShell

When you install VS Code, the default language mode for new files is set to plaintext. If you’re used to PowerShell ISE, this is quite annoying. In the way I work is that I open a new file, type my commands and then execute the code.

In VS Code you had to save the file first as a PowerShell file, or change the language mode before you could do this. David Wilson made it now possible to change this behaviour. He build in a new configuration setting called “files.defaultLanguage” which allows you to change the language mode for new, unsaved files.

Be carefull, that you specify the setting in lowercase powershell, otherwise it will not work.