# How to create & work with temporary file

**URL:** <https://forums.powershell.org/t/how-to-create-work-with-temporary-file/20189>\
**Category:** PowerShell Help\
**Created:** [August 16, 2022, 11:25pm UTC](https://forums.powershell.org/t/how-to-create-work-with-temporary-file/20189 "2022-08-16T23:25:50Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![DynV](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dynv/32/2643_2.png) [@DynV](https://forums.powershell.org/u/DynV)\
**Post date:** [August 16, 2022, 11:25pm UTC](https://forums.powershell.org/t/how-to-create-work-with-temporary-file/20189/1 "2022-08-16T23:25:50Z")

</div>

A program has multiple facets / sub-programs but it has no system to integrate those together; so it’s up to the user to make multiple commands when desiring to do a task with more than 1 sub-program. I could create files then delete them, but I’m wondering if there’s a way to create a script which those separate task could be put into a temporary file and link those sub-programs through those temporary files. For example:

> create temporary file T1, T2;  
> [1st command] $T1;  
> echo "1st command size: ", filesize ($T1);  
> [2nd command part 1] $T2 [2nd command part 2];  
> echo "2nd command size: ", filesize ($T2);  
> [3rd command part 1] $T1 [3rd command part 2] $T2 [3rd command part 3] output.ext;

In the above the only regular file created is output.ext.

Thank you kindly for your help

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [August 16, 2022, 11:47pm UTC](https://forums.powershell.org/t/how-to-create-work-with-temporary-file/20189/2 "2022-08-16T23:47:17Z")

</div>

DynV,  
Welcome to the forum. 👋🏽

To create a temporary file you can use

> **[New-TemporaryFile (Microsoft.PowerShell.Utility) - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/new-temporaryfile?view=powershell-7.2)**
>
> This cmdlet creates temporary files that you can use in scripts. The New-TemporaryFile cmdlet creates an empty file that has the .tmp file name extension. This cmdlet names the file tmp.tmp, where is a random hexadecimal number. The cmdlet creates...

Please read the help topic completely including the examples to learn how to use it.

But what exactly do you mean with

> [@DynV](#):
>
> How to … work with temporary file

?

---

<div class="post-metadata">

**Author:** ![DynV](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dynv/32/2643_2.png) [@DynV](https://forums.powershell.org/u/DynV)\
**Post date:** [August 17, 2022, 12:10am UTC](https://forums.powershell.org/t/how-to-create-work-with-temporary-file/20189/3 "2022-08-17T00:10:09Z")

</div>

> [@Olaf](#):
>
> But what exactly do you mean with

There’s an example in the OP.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [August 17, 2022, 12:14am UTC](https://forums.powershell.org/t/how-to-create-work-with-temporary-file/20189/4 "2022-08-17T00:14:55Z")

</div>

> [@DynV](#):
>
> There’s an example in the OP.

… an example of what? How can we help you? Could you elaborate a little more detailed? Did you read the help? Did it help you already?

---

<div class="post-metadata">

**Author:** ![DynV](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dynv/32/2643_2.png) [@DynV](https://forums.powershell.org/u/DynV)\
**Post date:** [August 17, 2022, 12:21am UTC](https://forums.powershell.org/t/how-to-create-work-with-temporary-file/20189/5 "2022-08-17T00:21:18Z")

</div>

> [@Olaf](#):
>
> How can we help you? Could you elaborate a little more detailed? Did you read the help?

Do you see the blockquote in the OP? I never created a powershell script before, I need to figure out how to do commands, as in Windows command prompt, with it before I can use the temporary file.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [August 17, 2022, 12:32am UTC](https://forums.powershell.org/t/how-to-create-work-with-temporary-file/20189/6 "2022-08-17T00:32:06Z")

</div>

> [@DynV](#):
>
> I never created a powershell script before

So you may start with learning the very basics of PowerShell first. 😉 It is beyond the scope of a forum like this to teach you how to write scripts.

I’d recommend to start with good book or an online tutorial or even some youtube videos would be useful to get you started.

---

<div class="post-metadata">

**Author:** ![DynV](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dynv/32/2643_2.png) [@DynV](https://forums.powershell.org/u/DynV)\
**Post date:** [August 17, 2022, 12:55am UTC](https://forums.powershell.org/t/how-to-create-work-with-temporary-file/20189/7 "2022-08-17T00:55:58Z")

</div>

I think I’ll be able to using [Batch convert videos using ffmpeg and powershell · GitHub](https://gist.github.com/sergiobd/95403ea3087373b661c3b72b59d75eb0) as an example. However, I I run “ffmpeg --version” in a command prompt it will give me an earlier version than I recently downloaded. How can I set the path of ffmpeg in the example? Something like ffmpeg.setpath([path]).

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [August 17, 2022, 1:00am UTC](https://forums.powershell.org/t/how-to-create-work-with-temporary-file/20189/8 "2022-08-17T01:00:16Z")

</div>

This is a PowerShell forum. I don’t know nothing about ffmpeg. But from a scripting point of view I’d recommend to use the full path instead if you want to use a particular version. 😉

---

<div class="post-metadata">

**Author:** ![DynV](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dynv/32/2643_2.png) [@DynV](https://forums.powershell.org/u/DynV)\
**Post date:** [August 17, 2022, 9:00pm UTC](https://forums.powershell.org/t/how-to-create-work-with-temporary-file/20189/9 "2022-08-17T21:00:43Z")

</div>

I’m trying to create the script step-by-step and thought the simplest one would be

> $fullPathFfmpeg = “C:\Users\Public\Libraries\programs\ffmpeg-5.1-essentials\_build\bin\ffmpeg.exe”
> 
> $fileInput1 = “PATH\_BEGINNING\Downloads\African senegal parrot talking-T9XC-csQA0o.mp4”  
> $fileInput2 = “PATH\_BEGINNING\Downloads\Alex African Grey Parrot talking to a Persian cat-0nIezlem7aw.mp4”  
> $fileOutput = “PATH\_BEGINNING\Videos\African\_senegal\_parrot\_talking-Alex\_African\_Grey\_Parrot\_talking\_to\_a\_Persian\_cat.mp4”
> 
> $fullPathFfmpeg -f concat -i “concat:$fileInput1|$fileInput2” -c copy $fileOutput

in a text file which I’m trying to run in the following

> PS PATH\_BEGINNING\Documents\> .\African\_senegal\_parrot\_talking-Alex\_African\_Grey\_Parrot\_talking\_to\_a\_Persian\_cat.ps1

that yielded

> At PATH\_BEGINNING\African\_senegal\_parrot\_talking-Alex\_African\_Grey\_Parrot\_talking\_to\_a\_Persian\_cat.p  
> s1:7 char:19
> 
> - $fullPathFfmpeg -f concat -i “concat:$fileInput1|$fileInput2” -c copy …
> - 
> 
> ```
> ~
> 
> ```
> 
> You must provide a value expression following the ‘-f’ operator.  
> At PATH\_BEGINNING\Documents\African\_senegal\_parrot\_talking-Alex\_African\_Grey\_Parrot\_talking\_to\_a\_Persian\_cat.p  
> s1:7 char:20
> 
> - $fullPathFfmpeg -f concat -i “concat:$fileInput1|$fileInput2” -c copy …
> - 
> 
> ```
> ~~~~~~
> 
> ```
> 
> Unexpected token ‘concat’ in expression or statement.  
> + CategoryInfo : ParserError: (🙂 , ParseException  
> + FullyQualifiedErrorId : ExpectedValueExpression

What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [August 17, 2022, 9:48pm UTC](https://forums.powershell.org/t/how-to-create-work-with-temporary-file/20189/10 "2022-08-17T21:48:01Z")

</div>

> [@DynV](#):
>
> What am I doing wrong?

- 1  
When you post code, sample data, console output or error messages please format it as code using the _preformatted text_ button ( **\</\>** ). Simply place your cursor on an empty line, click the button and paste your code.

Do not use the blockquote format! 👆🏽

Thanks in advance

[How to format code in PowerShell.org](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/b/bee50c628238f2c076c0504efb6b15f2fb11f002.gif) \<---- Click 👆🏽 😉

- 2  
Learn the basics of PowerShell. That includes how to read and understand error messages! 😉

- 3  
As you can read in the error message `-f` is a PowerShell operator. When you run an external tool using the same name as a command line argument you have to tell PowerShell not to parse it as PowerShell operator. You could use the stop parsing operator (`--%`). But you have to make sure you place it after all other things you want PowerShell to parse. 👆🏽  
I don’t know if that works but you could try this:

```auto
$fullPathFfmpeg -i "concat:$fileInput1|$fileInput2" -c copy $fileOutput --% -f concat 

```

You may have to escape the pipe symbol as well.

- 4  
Another option would be to use the `Start-Process` cmdlet

```auto
Start-Process -FilePath $fullPathFfmpeg -ArgumentList "-f concat -i 'concat:$fileInput1|$fileInput2' -c copy $fileOutput"

```

Again … you may have to escape the pipe symbol.

Please always read the help for the cmdlets you’re about to use completely including the examples.

At the moment you actually not using any PowerShell functionality. If it’s easier for you you may keep using batch/cmd files instead of PowerShell scripts.

> [@DynV](#):
>
> in a text file which I’m trying to run in the following

WHAT TEXT FILE??? Are you talking about a script? 🤔 A text file cannot run nothing because it’s just text.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:16pm UTC](https://forums.powershell.org/t/how-to-create-work-with-temporary-file/20189/11 "2024-05-16T20:16:44Z")

</div>

Closed on May 16, 2024, 8:16pm UTC


