When I right-click on folder or white-space inside folder, I want to have a menu option that will launch PowerShell in that location, then I want to run a function/alias which will perform the job of flattening the directory and overwrite if file with same name exists.

Update: Using get-help cmdlet on powershell, I managed to hack this command that flattens the working directory and overwrites if file exists.

gci -r -file | move -force -destination $PWD

Update: I added "Open with PowerShell" to context-menu by regedit

[HKEY_CLASSES_ROOT\Directory\Background\shell\Open with PowerShell]

[HKEY_CLASSES_ROOT\Directory\Background\shell\Open with PowerShell\command]
@="powershell.exe -noexit -command Set-Location '%V'"

[HKEY_CLASSES_ROOT\Directory\shell\Open with PowerShell]

[HKEY_CLASSES_ROOT\Directory\shell\Open with PowerShell\command]
@="powershell.exe -noexit -command Set-Location '%V'"

then adding a function was similar to many other languages:

function flatten { gci -r -file | move -force -destination $PWD }

But I can't seem to get this to automatically load when I open powershell. In bash, I would put this in .bashrc or .profile and it would be loaded.

I added profile.ps1 file with the function in $pshome, now I get this error:

. : File C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1 cannot be loaded because running scripts is disabled
on this system. For more information, see about_Execution_Policies at http://go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:3
+ . 'C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1'
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

Not sure if changing security setting is the only way around this.

How do I allow loading script/alias as safe as possible?


Changing command to entry below in regedit loads the function I need:

powershell.exe -noexit -command Set-Location '%V'; function flatten { gci -r -file | move -destination $PWD -force }

This is a workaround. I'm still interested in knowing how to load a profile without making risky changes to OS security.

  • 1
    I guess language recommendings are off-topic here (but use powershell). – SimonS Aug 26 '16 at 10:10
  • @SimonS language recommendation wouldn't be off topic. It would determine the next step. – user351764 Aug 26 '16 at 12:10
  • So, in the end, what is your question? How to create a command alias? Or perhaps how to add Powershell to the context menu? Please clarify and either remove this question and ask a new one or remove all the other stuff. – Daniel B Aug 27 '16 at 10:03
  • @DanielB I will edit and remove. Question is how to safely load alias/function from profile file with every powershell start. Since there is some useful information, I won't remove everything in regards to my previous question. – user351764 Aug 27 '16 at 10:17
  • @SimonS any powershell tips? – user351764 Aug 27 '16 at 13:37

See if this implementation of similar functionality helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy