How to use Powershell Profile with PSHELL

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
 

rconn

Administrator
Staff member
May 14, 2008
12,356
150
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
 
May 20, 2008
11,411
99
Syracuse, NY, USA
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
 
May 20, 2008
11,411
99
Syracuse, NY, USA
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
 
May 20, 2008
11,411
99
Syracuse, NY, USA
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
 

rconn

Administrator
Staff member
May 14, 2008
12,356
150
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"
 

rconn

Administrator
Staff member
May 14, 2008
12,356
150
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.
 
May 20, 2008
11,411
99
Syracuse, NY, USA
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'.
 

rconn

Administrator
Staff member
May 14, 2008
12,356
150
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
Thread starter Title Forum Replies Date
CWBillow TCC and TCMD in Powershell Support 6
Joe Caverly PowerShell=No Support 1
EDerH Powershell call from script Support 24
A Problem with functions @int @decimal and identifying Powershell as a shell. Support 12
Joe Caverly PowerShell Core Support 1
CWBillow PowerShell tab as Admin Support 2
CWBillow TCMD PowerShell Tab elevated Support 6
CWBillow Take Command tab as PowerShell Support 3
vefatica Powershell ... insert vs overstrike Support 2
CWBillow Powershell tab in TCMD? Support 2
Joe Caverly Fixed Displaying Profiles from TCC vs. PowerShell Support 2
F Powershell call fails with "Scripting Integrator 2016 (PowerShell)" license error Support 9
CWBillow PowerShell Tabs in TCMD Support 11
I v16 Spawning a PowerShell tab Support 3
vefatica "which" in PowerShell invokes TCC? Support 3
B Powershell scripts not opening in notepad-TC main suspect Support 2
electrotype Powershell in Take Command - Customizing colors? Support 4
C Attaching PowerShell changes its colors and makes it hard to read Support 15
CWBillow Backing up a user profile Support 7
vefatica FFIND /S in my profile directory Support 2
Joe Caverly PSHELL and COM Object in v27 Support 1
Joe Caverly PSHELL in v27 vs. 26 Support 17
vefatica @PSHELL? Support 5
Peter Murschall v27.00.17: PSHELL /S dosn't do nothing Support 8
Joe Caverly PSHELL and NTFS File Streams Support 2
vefatica PSHELL: How pass arguments to script? Support 15
Joe Caverly Using @PSHELL from @EVAL Support 8
vefatica PSHELL and @PSHELL fail the first time in a new TCC. Support 3
Peter Murschall Documentation Question to %@PSHELL Support 4
Joe Caverly PSHELL and @PSHELL errorlevel values Support 4
Joe Caverly PSHELL and out-of-process servers Support 2
Joe Caverly PSHELL /C Support 7
vefatica What happens to PSHELL after 20 seconds? Support 9
Joe Caverly .PS1 script does not work with PSHELL Support 11
samintz PSHELL's CWD Support 2
Joe Caverly Missing PSHELL in HELP Support 0
Joe Caverly PSHELL Blank Line Display Support 2
vefatica PSHELL? Support 6
Joe Caverly PSHELL and Receive-Job Support 4
Joe Caverly PSHELL Error? Support 16
Joe Caverly Documentation @PSHELL Support 0
M Fixed PSHELL command returns error Support 2

Similar threads