Showing posts with label file. Show all posts
Showing posts with label file. Show all posts

Monday, 3 May 2021

Write XML Files with Powershell - The Easy Way

 I needed to create a custom XML file using powershell as part of a script I was developing. I found it difficult to understand and use the in-built XML functionality with powershell so I attempted to simply use write-output commands to write the XML file. It worked - mostly, but the application I was submitting the XML file wasn't recognizing it. As it turns out there's some hidden properties within a properly formed XML file which requires a little trick to get right when forming it this way - so here's how I did it..

            $xmlfile = "C:\temp\test.xml"
            write-output '<?xml version="1.0"?>' | out-file "$xmlfile"
            write-output '<test>' | out-file "$xmlfile" -append
            write-output "  <version>1.0</version>" | out-file "$xmlfile" -append
            write-output '</test>' | out-file "$xmlfile" -append

            $xml = [xml](Get-Content $xmlfile)
            $xml.Save($xmlfile)

The first part of the script is fairly straightforward - declare the path to your XML file in the $xmlfile variable.

Then, use write-output commands to enter the text data into your XML file - make sure you include the -append argument after the first line to ensure the lines are appended onto the end of the file - you can open the file in a text editor such as NotePad++ to make sure it appears as you expect. Make sure you close off all your tags so that formatting is valid.

The last 2 lines is the trick I previously mentioned - use the [xml] class with get-content to import the XML file into the $xml variable. Once the data is in the $xml variable, it is then re-saved - these 2 lines are what puts the hidden formatting into the XML file to make it valid.


Wednesday, 7 September 2016

Restoring Files & Folders from Backup - Macrium Reflect

As outlined in my previous blog post, we have setup a full backup of your PC using a free and easy to use product called Macrium Reflect.

Once you have a full backup image file available, you can open, explore and restore files from it which is what we'll be covering in this blog post.


  1. Open windows explorer and browse to the location of your Macrium Backup image file (the file will have .mrimg on the end of it)
  2. Right click on the image file and select "Explore Image"

  3. Macrium will now load a "Backup Selection" screen where you can select which of the hard drives you backed up you wish to "mount" in windows explorer. In most cases (and this example), there will be one drive available (C:), so select it by placing a tick in the box next to it then click OK.

    You can also select the option to "Enable access to restricted folders" if you wish to access files that would normally be restricted (such as files in another users profile/account)
    Also note the "Drive Letter" that is specified - in this case H:\

  4. Windows Explorer will now mount the image and open Windows Explorer to show the files contained within it. As per the screenshot below, you can see H:\ is available and is called "Macrium Reflect Image". From here, you can view/browse all the files within the image. If you wish to restore/recover files from within this image, you can simply copy and paste them from this image drive, back to your C:\ or other folder as required

  5. Once you are done, you need to "unmount" the macrium image file. To do this, right click the macrium image drive letter (H:\ in this example), then go to go Macrium Reflect > Unmount Macrium Image

Tuesday, 29 January 2013

File Locked After Sending SMTP Email with Powershell

In my previous blog post about sending emails with Powershell, I mentioned a problem where files that you attach to an email become locked until the instance of Powershell you are running has exited completely. So if you run a script through Powershell ISE that attaches a file to an email, that file will remain locked until you exit Powershell ISE.

If the file is locked by Powershell, you will get an error/warning message similar to the following if you try to modify it in any way;

The process cannot access the file 'c:\filename.txt' because it is being used by another process

By using the following command, you can ensure that Powershell 'disposes' of the email message once it has been sent and does not continue to 'lock' any files you attach and send via email;

$mailmessage.dispose()

Note: this is assuming that $MailMessage = New-Object system.net.mail.mailmessage