Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - sourcing a file in a script

Reply
 
Old 08-31-2007   #1 (permalink)
JasonM


 
 

sourcing a file in a script

I'll probably kick myself after reading the answer, but for the life of me I
can't figure out how to source a file in PowerShell.

Specifically, I've got some files containing some variables common to some
scripts I'll be running. (file paths for example) Rather than inserting
them into each script and then having to do a large-scale hunt if/when they
change, I'd like to just have a one-liner to include their content. Plus
some of them will be maintained by somebody else so I would rather they not
have to edit the scripts.

Basically, what is PS's version of the Linix file sourcing or PHP's include()?

Thanks,
Jason

My System SpecsSystem Spec
Old 08-31-2007   #2 (permalink)
Kiron


 
 

Re: sourcing a file in a script

Dot-source the script:

.. <script>

# the space is optional

--
Kiron
My System SpecsSystem Spec
Old 08-31-2007   #3 (permalink)
Shay Levi


 
 

Re: sourcing a file in a script

You need to "Dot source" the file.
Basically, its ". <filename>".

Check "Get-ScriptDirectory to the Rescue" by Jeffrey Snover
http://blogs.msdn.com/powershell/arc...directory.aspx

Shay
http://scriptolog.blogspot.com


Quote:

> I'll probably kick myself after reading the answer, but for the life
> of me I can't figure out how to source a file in PowerShell.
>
> Specifically, I've got some files containing some variables common to
> some scripts I'll be running. (file paths for example) Rather than
> inserting them into each script and then having to do a large-scale
> hunt if/when they change, I'd like to just have a one-liner to include
> their content. Plus some of them will be maintained by somebody else
> so I would rather they not have to edit the scripts.
>
> Basically, what is PS's version of the Linix file sourcing or PHP's
> include()?
>
> Thanks,
> Jason

My System SpecsSystem Spec
Old 08-31-2007   #4 (permalink)
Kiron


 
 

Re: sourcing a file in a script

The space is optional when dot-sourcing from a variable:

$scripts= 'c:\dir1\script'

..$script

And it's required when dot-sourcing from a literal string

.. c:\dir1\script

Sorry for the misdirection

--
Kiron
My System SpecsSystem Spec
Old 08-31-2007   #5 (permalink)
Shay Levi


 
 

Re: sourcing a file in a script

Expanding on Kiron's great samples:

param([string]$scriptToSource)
..$scriptToSource


Shay
http://scriptolog.blogspot.com


Quote:

> The space is optional when dot-sourcing from a variable:
>
> $scripts= 'c:\dir1\script'
>
> .$script
>
> And it's required when dot-sourcing from a literal string
>
> . c:\dir1\script
>
> Sorry for the misdirection
>


My System SpecsSystem Spec
Old 08-31-2007   #6 (permalink)
JasonM


 
 

Re: sourcing a file in a script

Yeah, commence kicking.
I got off on a wild mental tanget and could not pull myself back.

Thanks.
Jason
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Dot-sourcing and scope PowerShell
problem passing args to script 'There is no script engine for file extenstion' VB Script
Dot sourcing multiple code files PowerShell
Script file has 'OS Handle' error when run from script PowerShell
Can you drag-n-drop a file on top of a PS script to run the script? PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46