Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

How to use Powershell Profile with PSHELL

Aug
1,914
68
Further to https://jpsoft.com/forums/threads/powershell-profile.8776/#post-49432

Code:
Windows PowerShell
Copyright (C) 2016 Microsoft Corporation. All rights reserved.

PS C:\utils> $Profile
C:\Users\jlc\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

Example function in my PowerShell Profile;
Code:
function today
{
  gci | where { $_.LastWriteTime.Date -eq (Get-Date).Date }
}

From PowerShell;
Code:
PS C:\utils> today


    Directory: C:\utils


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        1/16/2018   6:37 AM           8766 astronomy.txt
-a----        1/16/2018   7:12 AM          24410 bandwidth.txt
-a----        1/16/2018   5:03 AM          33327 jlckcalc.txt
-a----        1/16/2018   1:45 PM            843 killstuff.txt
-a----        1/16/2018   7:12 AM          16128 ll.txt
-a----        1/16/2018   5:01 AM          25026 tackcalc.txt

From TCC 22.00.33 x64 Windows 7 [Version 6.1.7601]
Code:
c:\users\jlc\utils>powershell -command today


    Directory: C:\Users\jlc\utils


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        1/16/2018   6:37 AM           8766 astronomy.txt
-a----        1/16/2018   7:12 AM          24410 bandwidth.txt
-a----        1/16/2018   5:03 AM          33327 jlckcalc.txt
-a----        1/16/2018   1:45 PM            843 killstuff.txt
-a----        1/16/2018   7:12 AM          16128 ll.txt
-a----        1/16/2018   5:01 AM          25026 tackcalc.txt

From TCC 22.00.33 x64 Windows 7 [Version 6.1.7601]
Code:
c:\users\jlc\utils>pshell /s "today"
PSHELL: System.Management.Automation.CommandNotFoundException : The term 'today' 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.

How do I call a function that is in my PowerShell Profile, that works in PowerShell, using the TCC PSHELL command, since "TCC already uses PowerShell profiles"?

Joe
 
Well, I found out why PSHELL does not load my PowerShell $Profile.

I did the following from TCC;
Code:
c:\users\jlc\utils>pshell /s "& $Profile"
PSHELL: System.Management.Automation.SetValueInvocationException : Exception setting "backgroundcolor": "A command that prompts the user failed because the host program or the command type does not support user interaction. Try a host program that supports user interaction, such as the Windows PowerShell Console or Windows PowerShell ISE, and remove prompt-related commands from command types that do not support user interaction, such as Windows PowerShell workflows."

What is "backgroundcolor"?

I have the following near the beginning of my $Profile;
Code:
$a = (get-host).ui.rawui    
$a.backgroundcolor = "black"
$a.foregroundcolor = "green"

#ing theses commands out allows my $Profile to be used by PSHELL, but now, those commands do not load in PowerShell, since I had to # them out for PSHELL.

Any ideas for a workaround?

Joe
 
I have devised a work-around.

In my PowerShell $Profile, I have "wrapped" the code that PSHELL does not like;
Code:
$PSPid = Get-Process PowerShell
if ($pid -eq $PSPid.Id)
{
$a.backgroundcolor = "black"
$a.foregroundcolor = "green"
}
Remove-Variable PSPid

Hoping that this can be fixed in TCC, so that this modification is not necessary.

Joe
 
The PowerShell class in TCC will look for profiles in the following locations:
  • %windir%\system32\WindowsPowerShell\v1.0\profile.ps1
  • %windir%\system32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1
  • %UserProfile%\My Documents\WindowsPowerShell\profile.ps1
  • %UserProfile%\My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
 
Hmmm! I have two PS profiles in one of those places. Both define the alias "e" (TextPad) and that alias works in a PS instance, but not in a PSHELL command.

1516247038249.png
 
I see some other strange things. Here's one. PS doesn't echo the name of the file after typing it.

1516248088609.png


And this one echoes the name, types the file 3 times, and echoes its name again!

1516248178535.png
 
Now I'm seeing evidence that my profile file is read. The alias ("e") is set (so shows "PSHELL /S alias");

AFAICT, nearly all strings (PSHELL /S") work better when quoted, but things go haywire when there are quotes internal to the string.

How do I use PSHELL to ask PS to execute this command?
Code:
l:\textpad5\textpad.exe "c:\Users\vefatica\My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1"
 
Now I'm seeing evidence that my profile file is read. The alias ("e") is set (so shows "PSHELL /S alias");

AFAICT, nearly all strings (PSHELL /S") work better when quoted, but things go haywire when there are quotes internal to the string.

How do I use PSHELL to ask PS to execute this command?
Code:
l:\textpad5\textpad.exe "c:\Users\vefatica\My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1"

Single quotes are your friend.

Code:
pshell /s "I:\textpad5\textpad.exe 'c:\Users\vefatica\My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1'"

Although, I would use;
Code:
pshell /s "I:\textpad5\textpad.exe $Profile"

In my PowerShell profile, I have an alias defined;
Code:
new-item alias:npp -value C:\Progra~1\Notepad++\notepad++.exe > $null

I also have an alias for NotePad defined in TCC;
Code:
alias npp=C:\Progra~1\Notepad++\notepad++.exe

Now, from TCC, I can do the following;
Code:
pshell /s "npp $Profile"

That way, I am using the same editor in both TCC and PowerShell.

Joe
 
I see some other strange things. Here's one. PS doesn't echo the name of the file after typing it.

And this one echoes the name, types the file 3 times, and echoes its name again!

They work if you use the correct syntax and enclose the command in double quotes. PSHELL supports multiple commands for -S and -F; without the quotes PSHELL thinks your single command is two or more.

pshell /s "type $Profile"

pshell /s "$Profile ; type $Profile"
 
Now I'm seeing evidence that my profile file is read. The alias ("e") is set (so shows "PSHELL /S alias");

AFAICT, nearly all strings (PSHELL /S") work better when quoted, but things go haywire when there are quotes internal to the string.

How do I use PSHELL to ask PS to execute this command?
Code:
l:\textpad5\textpad.exe "c:\Users\vefatica\My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1"

PSHELL only removes the outer enclosing quotes before passing the command to PowerShell.
 
PSHELL only removes the outer enclosing quotes before passing the command to PowerShell.
Only the outer ones? The command below without the outer quotes works without error in Powershell. As below, when used with PSHELL, the editor starts with the correct file, but there's also an error message.

Code:
v:\> pshell /s "e "c:\Users\vefatica\My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1""
PSHELL: System.Management.Automation.CommandNotFoundException : The module 'Documents' could not be loaded. For m
ore information, run 'Import-Module Documents'.
 
Only the outer ones? The command below without the outer quotes works without error in Powershell. As below, when used with PSHELL, the editor starts with the correct file, but there's also an error message.

Code:
v:\> pshell /s "e "c:\Users\vefatica\My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1""
PSHELL: System.Management.Automation.CommandNotFoundException : The module 'Documents' could not be loaded. For m
ore information, run 'Import-Module Documents'.

Yes, only the outer ones.

The error is coming from PowerShell, not TCC. Did you try importing the requested module?
 

Similar threads

Back
Top