# Powershell Get Scripts in Batch script and output to log file

**URL:** <https://forums.powershell.org/t/powershell-get-scripts-in-batch-script-and-output-to-log-file/22104>\
**Category:** PowerShell Help\
**Created:** [April 11, 2023, 2:21pm UTC](https://forums.powershell.org/t/powershell-get-scripts-in-batch-script-and-output-to-log-file/22104 "2023-04-11T14:21:53Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![sqldba20](https://avatars.discourse-cdn.com/v4/letter/s/dfb087/32.png) [@sqldba20](https://forums.powershell.org/u/sqldba20)\
**Post date:** [April 11, 2023, 2:21pm UTC](https://forums.powershell.org/t/powershell-get-scripts-in-batch-script-and-output-to-log-file/22104/1 "2023-04-11T14:21:53Z")

</div>

I need help with powershell batch script. I have these Backup IIS and 5 powershell Get Scripts that I want to run in powershell batch. I also want the output of all these redirected to Log File and also pass the “website name” as parameters.

```auto
#BACKUP IIS
$folderExists = Test-Path -Path $env:Windir\System32\inetsrv\backup\IISBackup

if($folderExists)
{
    $timestamp = Get-Date -Format "yyyyMMdd hhmmss"

    $newName = "IISBackup" + $timestamp

    Rename-Item -Path $env:Windir\System32\inetsrv\backup\IISBackup -NewName $newName
}

Backup-WebConfiguration -Name "IISBackup"

#Display Current Configuration
Get-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/<website name>' -filter 'system.web/authentication/forms' -name 'requireSSL'
Get-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/<website name>' -filter 'system.web/authentication/forms' -name 'cookieless'
Get-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/<website name>' -filter 'system.web/authentication/forms' -name 'protection'
Get-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -location '' -filter 'system.webServer/security/access' -name 'sslFlags'
Get-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/' -filter 'system.web/authentication/forms/credentials' -name 'passwordFormat'

```

---

<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:** [April 11, 2023, 2:26pm UTC](https://forums.powershell.org/t/powershell-get-scripts-in-batch-script-and-output-to-log-file/22104/2 "2023-04-11T14:26:05Z")

</div>

Raj,  
Welcome to the forum. 👋🏼

When you crosspost the same question at the same time to different forums you should at least post links to the other forums along with your question to avoid people willing to help you spending potentially a lot of effort in vain. 😉

> **[Powershell Get Scripts in Batch script and output to log file - Microsoft...](https://learn.microsoft.com/en-us/answers/questions/1215233/powershell-get-scripts-in-batch-script-and-output)**
>
> I need help with powershell batch script. I have these 5 powershell Get Scripts that I want to run in powershell batch. I also want the output of all these redirected to Log File and also pass the as…

---

<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:** [April 11, 2023, 2:31pm UTC](https://forums.powershell.org/t/powershell-get-scripts-in-batch-script-and-output-to-log-file/22104/3 "2023-04-11T14:31:56Z")

</div>

> [@sqldba20](#):
>
> I also want the output of all these redirected to Log File

You simply pipe the output to …

> **[Out-File (Microsoft.PowerShell.Utility) - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/out-file?view=powershell-7.3)**
>
> The Out-File cmdlet sends output to a file. It implicitly uses PowerShell's formatting system to write to the file. The file receives the same display representation as the terminal. This means that the output may not be ideal for programmatic...

> [@sqldba20](#):
>
> pass the “website name” as parameters.

Here y oucan read about how to use parameters

> **[about Functions Advanced - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced?view=powershell-7.3)**
>
> Introduces advanced functions that are a way to create cmdlets using scripts.

---

<div class="post-metadata">

**Author:** ![sqldba20](https://avatars.discourse-cdn.com/v4/letter/s/dfb087/32.png) [@sqldba20](https://forums.powershell.org/u/sqldba20)\
**Post date:** [April 11, 2023, 2:42pm UTC](https://forums.powershell.org/t/powershell-get-scripts-in-batch-script-and-output-to-log-file/22104/4 "2023-04-11T14:42:58Z")

</div>

So, the Out-File command should be specified after each command? Something like this? Will it append?

```auto

#BACKUP IIS
$folderExists = Test-Path -Path $env:Windir\System32\inetsrv\backup\IISBackup

if($folderExists)
{
    $timestamp = Get-Date -Format "yyyyMMdd hhmmss"

    $newName = "IISBackup" + $timestamp

    Rename-Item -Path $env:Windir\System32\inetsrv\backup\IISBackup -NewName $newName
}

Backup-WebConfiguration -Name "IISBackup" | Out-File -FilePath C:\Temp\IISWebConfiguationFile.Log

#Display Current Configuration
Get-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/<website name>' -filter 'system.web/authentication/forms' -name 'requireSSL' | Out-File -FilePath C:\Temp\IISWebConfiguationFile.Log -Append
Get-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/<website name>' -filter 'system.web/authentication/forms' -name 'cookieless' | Out-File -FilePath C:\Temp\IISWebConfiguationFile.Log -Append
Get-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/<website name>' -filter 'system.web/authentication/forms' -name 'protection' | Out-File -FilePath C:\Temp\IISWebConfiguationFile.Log -Append
Get-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -location '' -filter 'system.webServer/security/access' -name 'sslFlags' | Out-File -FilePath C:\Temp\IISWebConfiguationFile.Log -Append
Get-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/' -filter 'system.web/authentication/forms/credentials' -name 'passwordFormat' | Out-File -FilePath C:\Temp\IISWebConfiguationFile.Log -Append

```

---

<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:** [April 11, 2023, 2:57pm UTC](https://forums.powershell.org/t/powershell-get-scripts-in-batch-script-and-output-to-log-file/22104/5 "2023-04-11T14:57:43Z")

</div>

> [@sqldba20](#):
>
> So, the Out-File command should be specified after each command? Something like this? Will it append?

Would it hurt if you tried it? 😉

---

<div class="post-metadata">

**Author:** ![sqldba20](https://avatars.discourse-cdn.com/v4/letter/s/dfb087/32.png) [@sqldba20](https://forums.powershell.org/u/sqldba20)\
**Post date:** [April 11, 2023, 3:29pm UTC](https://forums.powershell.org/t/powershell-get-scripts-in-batch-script-and-output-to-log-file/22104/6 "2023-04-11T15:29:50Z")

</div>

I guess my question was, is that the only option where we specify Out-File after each command?

---

<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:** [April 11, 2023, 3:48pm UTC](https://forums.powershell.org/t/powershell-get-scripts-in-batch-script-and-output-to-log-file/22104/7 "2023-04-11T15:48:54Z")

</div>

Why? What’s wrong with it?

---

<div class="post-metadata">

**Author:** ![sqldba20](https://avatars.discourse-cdn.com/v4/letter/s/dfb087/32.png) [@sqldba20](https://forums.powershell.org/u/sqldba20)\
**Post date:** [April 11, 2023, 4:13pm UTC](https://forums.powershell.org/t/powershell-get-scripts-in-batch-script-and-output-to-log-file/22104/8 "2023-04-11T16:13:45Z")

</div>

Nothing is wrong. I wanted to know whether there is any easier option. I guess not. Thanks!

---

<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:** [April 11, 2023, 4:18pm UTC](https://forums.powershell.org/t/powershell-get-scripts-in-batch-script-and-output-to-log-file/22104/9 "2023-04-11T16:18:25Z")

</div>

> [@sqldba20](#):
>
> whether there is any easier option

It depends on what you consider “_easier_”. 😏 … in the vast majority of the cases there are at least a few different options to achieve a goal.

---

<div class="post-metadata">

**Author:** ![sqldba20](https://avatars.discourse-cdn.com/v4/letter/s/dfb087/32.png) [@sqldba20](https://forums.powershell.org/u/sqldba20)\
**Post date:** [April 11, 2023, 4:26pm UTC](https://forums.powershell.org/t/powershell-get-scripts-in-batch-script-and-output-to-log-file/22104/10 "2023-04-11T16:26:15Z")

</div>

I am getting error on this. Is it because the $websitename is between the single quotes? Any idea on this?

```auto
$websitename = $args[0]

Get-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/$websitename' -filter 'system.web/authentication/forms' -name 'requireSSL' | Out-File -FilePath C:\Temp\IISWebConfiguationFile.Log -Append

```

---

<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:** [April 11, 2023, 5:00pm UTC](https://forums.powershell.org/t/powershell-get-scripts-in-batch-script-and-output-to-log-file/22104/11 "2023-04-11T17:00:22Z")

</div>

> [@sqldba20](#):
>
> Is it because the $websitename is between the single quotes?

Yes. Could you please at least try to search for a solution first by yourself before you come here and ask for help? Really often it will take only seconds to find a solution because in the vast majority of the cases you’re not the very first one with a given task or with the same or a similar issue with your code.

> **[about Quoting Rules - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules?view=powershell-7.3)**
>
> Describes rules for using single and double quotation marks in PowerShell.

Variables will not be expanded in single quotes.

---

<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:14pm UTC](https://forums.powershell.org/t/powershell-get-scripts-in-batch-script-and-output-to-log-file/22104/12 "2024-05-16T20:14:56Z")

</div>

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


