Lesson learned while using Taskfile to invoke PowerShell command

Lesson learned while using Taskfile to invoke PowerShell command

Context

I recently came across TaskFile and wanted to use it to automate some of my workflows, which underlying invokes PowerShell commands. However, I faced several errors while doing so, and this post is to reflect on it, and hopefully help myself (in the future), or someone else.

To be clear, this has nothing to do with TaskFile.

Current State

I wrote a super simple taskfile.yml which invokes my PowerShell script.

version: '3'

tasks:
  dl-apps:
    platforms: [windows]
    dir: /apps
    cmds:
      - powershell -File apps-downloads.ps1

Attempt #1

I try to run the task command, but encounter the following error message.

> task dl-apps
task: [dl-apps] powershell -File apps-downloads.ps1
Import-Module : The specified module 'oh-my-posh' was not loaded because no valid module file was found in any module directory.
At C:\Users\Joseph\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:2 char:1
+ Import-Module oh-my-posh
+ ~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (oh-my-posh:String) [Import-Module], FileNotFoundException
    + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand

Import-PowerShellDataFile : The term 'Import-PowerShellDataFile' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was
included, verify that the path is correct and try again.
At C:\Users\Joseph\Documents\PowerShell\Modules\Terminal-Icons\0.9.0\Terminal-Icons.psm1:210 char:33
+         $hash.Add($_.Basename, (Import-PowerShellDataFile $_.FullName ...
+                                 ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Import-PowerShellDataFile:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

It wasn't immediately obvious to me what the problem was, so I attempted to figure out what was causing it.

Wrong PowerShell Profile invoked?

Running this command would show the current profiles

> $PROFILE | Select-Object *

AllUsersAllHosts       : C:\Program Files\PowerShell\7\profile.ps1
AllUsersCurrentHost    : C:\Program Files\PowerShell\7\Microsoft.PowerShell_profile.ps1
CurrentUserAllHosts    : C:\Users\Joseph\Documents\PowerShell\profile.ps1
CurrentUserCurrentHost : C:\Users\Joseph\Documents\PowerShell\Microsoft.PowerShell_profile.ps1
Length                 : 69

Only CurrentUserCurrentHost is valid as I did not find any profiles (.ps1) file

I later realized that it did not display the list of profiles for legacy PowerShell, those in <path>\WindowsPowerShell\

My Microsoft.PowerShell_profile.ps1 contains the following

Import-Module Terminal-Icons
Import-Module PSReadLine
Set-PSReadLineOption -PredictionSource History
Set-PSReadLineOption -PredictionViewStyle ListView
Set-PSReadLineOption -EditMode Windows

Set-Alias k kubectl
Set-Alias mk minikube

oh-my-posh init pwsh --config "$env:POSH_THEMES_PATH\night-owl.omp.json" | Invoke-Expression

There was no indication of posh-git module. I have already removed it and changed it to using the oh-my-posh installation method rather than using the PowerShell module.

So I'm quite confused about why this happens.

Wrong posh-git invoked?

So I ran the following command to see if there's posh-git and it does exist

> Get-module -ListAvailable

    Directory: C:\Users\Joseph\Documents\PowerShell\Modules

ModuleType Version    PreRelease Name                                PSEdition ExportedCommands
---------- -------    ---------- ----                                --------- ----------------
Script     1.0.0                 posh-git                            Desk      {Add-PoshGitToProfile, Expand-GitCommand, Format-GitBranchName, Get-GitBranchStatusColor…}
Script     2.2.1      rc1        PSReadLine                          Desk      {Get-PSReadLineKeyHandler, Set-PSReadLineKeyHandler, Remove-PSReadLineKeyHandler, Get-PSReadLineOption…}
Script     0.9.0                 Terminal-Icons                      Desk      {Add-TerminalIconsColorTheme, Add-TerminalIconsIconTheme, Format-TerminalIcons, Get-TerminalIconsColorTheme…}
Script     1.1.13                z                                   Desk      {z, cdX, popdX, pushdX}

That's rather interesting as I thought I had already removed it. So I uninstalled it again

> Uninstall-Module posh-git

After which, I verified that it was indeed removed.

> Get-module -ListAvailable

    Directory: C:\Users\Joseph\Documents\PowerShell\Modules

ModuleType Version    PreRelease Name                                PSEdition ExportedCommands
---------- -------    ---------- ----                                --------- ----------------
Script     2.2.1      rc1        PSReadLine                          Desk      {Get-PSReadLineKeyHandler, Set-PSReadLineKeyHandler, Remove-PSReadLineKeyHandler, Get-PSReadLineOption…}
Script     0.9.0                 Terminal-Icons                      Desk      {Add-TerminalIconsColorTheme, Add-TerminalIconsIconTheme, Format-TerminalIcons, Get-TerminalIconsColorTheme…}
Script     1.1.13                z                                   Desk      {z, cdX, popdX, pushdX}

Attempt #2

With that, I tried to run the task command again, however, it is still throwing the same error.

> task dl-apps
task: [dl-apps] powershell -File apps-downloads.ps1
Import-Module : The specified module 'posh-git' was not loaded because no valid module file was found in any module directory.
At C:\Users\Joseph\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1:1 char:1
+ Import-Module posh-git
+ ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (posh-git:String) [Import-Module], FileNotFoundException
    + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand

Since I just uninstalled posh-git, and have verified it. This error shouldn't happen anymore. So I printed out the content of the file which in hindsight, I should have done in the first place.

> cat C:\Users\Joseph\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

And this is the output

> cat C:\Users\Joseph\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
Import-Module posh-git
Import-Module oh-my-posh
Import-Module Terminal-Icons
Import-Module PSReadLine
Import-Module z
Set-PSReadLineOption -PredictionSource History
Set-PSReadLineOption -PredictionViewStyle ListView
Set-PSReadLineOption -EditMode Windows
Set-PoshPrompt Paradox

Which then made me realize that I was looking at two different files right from the start!

# PS - Legacy < 5.x
C:\Users\Joseph\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
# PS - Current > 6.x
C:\Users\Joseph\Documents\PowerShell\Microsoft.PowerShell_profile.ps1

So there are two different versions of the directory, and running for different PowerShell versions.

Solution

Knowing my mistake, it was quite simple to fix it. I just had to switch to using pwsh command. But I guess this is also a good lesson for me to always take closer look at the differences between versions.

version: '3'

tasks:
  dl-apps:
    platforms: [windows]
    dir: /apps
    cmds:
      - pwsh -File apps-downloads.ps1

References: