Scheduled Task for running PowerShell Script Function

TonyJK 876 Reputation points
2020-12-06T22:16:35.047+00:00

Hi,

In order to run a specific Function in PowerShell, I was told

  1. Dot Source the Powershell script - . .\FunctionScript.PS1
  2. Run the script - .\FunctionScript.PS1
  3. Run the Function - FunctionScript

I also find that another fellow suggest that we can run a scheduled task for function as follow
powershell -command ". c:\functions.ps1; manual "

Since we need to Dot Source the script, does the scheduled task work or not ?

Thanks

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,383 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ian Xue (Shanghai Wicresoft Co., Ltd.) 30,361 Reputation points Microsoft Vendor
    2020-12-07T08:39:23.733+00:00

    Hi,

    Only the powershell session created by the scheduled task will dot source the script. If you want your powershell to dot source the script automatically you can add . c:\functions.ps1 to the powershell profile $Home[My ]Documents\WindowsPowerShell\Profile.ps1

    https://devblogs.microsoft.com/scripting/understanding-the-six-powershell-profiles/

    Best Regards,
    Ian

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.