Link to home
Start Free TrialLog in
Avatar of DevSupport
DevSupport

asked on

outfile in current directory of powershell script

How do I put the outfile in the current directory where ps script is located.

I'm trying something like this but doesnt work

$PSScriptRoot = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
Out-File $PSScriptRoot + '\output1.txt'

ps version 4.

Please help.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DevSupport
DevSupport

ASKER

Perfect!
This:
Out-File $PSScriptRoot + '\output1.txt'

Open in new window

leads to a run-time error because the + is interpreted as parameter (for encoding).
Ths works:
$PSScriptRoot = Split-Path -Path $MyInvocation.MyCommand.Definition
# or
$PSScriptRoot = $MyInvocation.MyCommand.Path.DirectoryName
Out-File ($PSScriptRoot + '\output1.txt')

Open in new window