Temporary file with given extension.

Creating temporary file in PowerShell is pretty simple – because you can use .NET call directly it’s a matter of:


The problem you may walk into is the rare case when your file has to have specific extension to work properly. I usually have this issue when I try to use temporary file to create formatting/ type information at runtime. If you leave .tmp extension, you can not use it for Update-*Data:

Cannot read file "C:\Users\bielawb\AppData\Local\Temp\tmp3069.tmp"  because it does not have the extension "ps1xml".

Obviously – you need to rename the file. Nothing really tricky here. But with PowerShell’s pipeline model you can do all that in one go instead of juggling variables:

[IO.Path]::GetTempFileName() |             
    Rename-Item -NewName { $_ -replace 'tmp$', 'ps1xml' }PassThru

You will get FileInfo object back, and it will have correct extension for *Data files. All you need to do is put your XML in it and change your environment to suite your needs. Puszczam oczko

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s