Sondre
Sondre

Reputation: 13

Powershell function doesnt work after restart?

My functions work fine before restart but not after. Why?

Code:

Function import-cloud {
    Robocopy "C:\Users\sonde\2021sky" "C:\Users\sonde\Skrivebord\2021lokal" /MIR
}
Function export-cloud { 
    Robocopy "C:\Users\sonde\Skrivebord\2021lokal" "C:\Users\sonde\2021sky" /MIR
}

enter image description here

Upvotes: 0

Views: 227

Answers (1)

filimonic
filimonic

Reputation: 4644

Cmdlets or Functions are not loaded normally during Powershell startup.

On start:

  1. Powershell loads descriptors of modules from $env:PSModulePath directory to be ready to internally call import-module when you try to use CmdLet from one of this modules. This is called Auto-Loading and it's a little bit tricky, so only module vendors use this.

  2. Then Powershell executes file $Profile it it exists. If you define anything here, it will be executed. If you define function here, It will be executed (for functions this means storing in RAM and be ready to use)


So as most easy way, you should create a $profile file and paste those functions here:

[void][System.IO.Directory]::CreateDirectory([System.IO.Path]::GetDirectoryName($profile))
If (-not [System.IO.File]::Exists($profile)) { '' | Out-File $profile }
Start-Process -Verb 'Edit' -FilePath $profile

Upvotes: 1

Related Questions