Set the font type and size using the command prompt (or batch file) - powershell

I tried the solution given from: Specify the size of command prompt when executing a batch file
I ran:
powershell -command "&{set-executionpolicy remotesigned; Import-Module SetConsoleFont; Get-ConsoleFontInfo | Format-Table -AutoSize}"
But I get these errors, any ideas?
Set-ExecutionPolicy : Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' is denied. At line:1 char:22 + &{set-executionpolicy <<<< remotesigned; Import-Module SetConsoleFont; Get-ConsoleFontInfo | Format-Table -AutoSize} + CategoryInfo : NotSpecified: (:) [Set-ExecutionPolicy], UnauthorizedAccessException + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand
Import-Module : The specified module 'SetConsoleFont' was not loaded because no valid module file was found in any module directory . At line:1 char:50 + &{set-executionpolicy remotesigned; Import-Module <<<< SetConsoleFont; Get-ConsoleFontInfo | Format-Table -AutoSize} + CategoryInfo : ResourceUnavailable: (SetConsoleFont:String) [Import-Module], FileNotFoundException + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
The term 'Get-ConsoleFontInfo' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spe lling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:86 + &{set-executionpolicy remotesigned; Import-Module SetConsoleFont; Get-ConsoleFontInfo <<<< | Format-Table -AutoSize} + CategoryInfo : ObjectNotFound: (Get-ConsoleFontInfo:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException
I have put the file SetConsoleFont.psm1 in
C:\Users\Adrian\Documents\WindowsPowerShell\Modules\SetConsoleFont
You say "You're not allowed to set the execution policy" well maybe I'm not, but it's my machine so why shouldn't I? I don't want to execute these commands as Administrator, just as a user, me (Adrian)
Another comment was to try set-executionpolicy bypass process
so I tried:
powershell -command "&{set-executionpolicy bypass process; set-executionpolicy remotesigned; Import-Module SetConsoleFont; Get-ConsoleFontInfo | Format-Table -AutoSize}"
But got even more red errors.
I have no idea what powershell is or how to use it, I just want to change the font from a batch file without hassle!

Try set-executionpolicy bypass process instead.
Also make sure you have put the module in a module path folder such as:
[yourprofile]\Documents\WindowsPowershell\Modules

I managed to get it working but only in a PowerShell console, and I had to run it as Administrator. However this is not practical for me for the following reasons:
I wish to change the font of new window seamlessly from a batch file, which will be run by users of the software. They may not have Administrator access and so cannot execute "set-executionpolicy remotesigned" which I needed to do to get it working.
Also this has to be done in a DOS batch file, so opening up a powershell window is not an option. It only works in a PowerShell window and not with the DOS "powershell -command" option.
So a partial answer.

If you want to change Execution Policy, it should be done in an elevated prompt.
And loading the module can be done by giving absolute path. Example is below.
Import-Module c:\users\testuser\desktop\SetConsoleFont.psm1 -Verbose
and we can bypass execution policy like below.
powershell.exe -executionpolicy bypass -command "${<your code>}"
Edit: The imported module will be available only in the scope of the script block.
here it is with in {}. So whatever cmdlets and functions in side the module should be executed in sided the scriptblock.
Regards,
Kvprasoon

Related

Why am I getting errors when trying to run Get-Transportconfig in a Powershell session?

I am trying to run Powershell to invoke Set-TransportConfig on my Exchange mailbox.
Windows Powershell ISE is running elevated as admin.
I am able to connect to Exchange:
Connect-ExchangeOnline -UserPrincipalName
When I attempt to run this command:
Get-TransportConfig -SmtpClientAuthenticationDisabled
I get the following error:
Get-TransportConfig : The term 'Get-TransportConfig' 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 line:1 char:1
Get-TransportConfig -SmtpClientAuthenticationDisabled
+ CategoryInfo : ObjectNotFound: (Get-TransportConfig:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
If I run the following:
get-command *transportconfig*
It does not return any commands:.
I've tried running:
Import-Module ExchangeOnlineManagement
Install-Module ExchangeOnlineManagement
It does not return any errors or results (Does that mean it's correctly installed?)
Why can't I run the Get-TransportConfig command? What am I doing wrong here? Am I missing an additional library?

Activate a virtual environment in Windows Powershell with a command that is equivalent to "source"

I need to activate a virtual environment in Windows Powershell. But, the guide i am using that to proceed my project tells me to use a command like this:
source venv-slither/bin/activate
In my project directory, in first, i installed virtualenv with:
pip3 install virtualenv
After that, I setup a virtual environment in folder ./venv-slither with:
source venv-slither/bin/activate
Problem emerges here. Whenever i run the last command i approaches to this message in Powershell:
source : The term 'source' 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 line:1 char:2
+ source venv-slither/bin/activate
+ ~~~~~~
+ CategoryInfo : ObjectNotFound: (source:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
I read some guide to eliminate this. They suggest using . instead source, but by this way the same error occurs. When, my Command is:
. venv-slither/bin/activate
The message is:
. : The term 'venv-slither/bin/activate' 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 line:1 char:3
+ . venv-slither/bin/activate
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (venv-slither/bin/activate:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
More over, when i change slashes direct:
. venv-slither\bin\activate
This message comes up:
. : The module 'venv-slither' could not be loaded. For more information, run 'Import-Module venv-slither'.
At line:1 char:3
+ . venv-slither\bin\activate
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (venv-slither\bin\activate:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CouldNotAutoLoadModule
I have these modules in my project directory: .bin, dot, findit, source.
Please help a beginner man in working with Windows Powershell.
I think you should use like this, within Windows PS:
venv-slither/Scripts/activate.ps1
After enabling the necessary execution policy for the current user with this command:
Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope CurrentUser
I would return to previous policy with this command, after you finish your work, for safety:
Set-ExecutionPolicy -ExecutionPolicy Undefined -Scope CurrentUser
There are other ways to set the execution temporarily. For example, I can set for the current PS's session by issuing this command:
powershell.exe -ExecutionPolicy RemoteSigned
* RemoteSigned or Unrestricted, you can refer to this link to view the details which scripts are or not allowed for each type of policy:
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-7.1
Sorry for posting too late, I've just run into your post when found the same issue, tested myself and it worked.

Not able to execute the "Import-module"

I get below error when i try to execute the command "Import-module script.ps1"
any idea why is that shows up? what need to be done to resolve the issue ?
PS C:\File> Import-module script.ps1
Import-module : The specified module 'script.ps1' was not loaded because no valid module file was found in any module directory.
At line:1 char:1
+ Import-module script.ps1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (script.ps1:String) [Import-Module], FileNotFoundException
+ FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
You need to use .\ to execute scripts, commands from current directory. i.e. ipmo .\script.ps1
As a security feature, PowerShell does not run executable (native)
commands, including PowerShell scripts, unless the command is located
in a path that is listed in the Path environment variable $env:path or
unless you specify the path to the script file.
To run a script that is in the current directory, specify the full
path, or type a dot .\ to represent the current directory.
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_command_precedence?view=powershell-7

Import-Module stops working sporadically

I have a problem where Import-Module -Name "./MyAssembly.dll" stops working sporadically. If I restart the computer, everything seems to work as normal for a while, but it's getting really frustrating.
The reason for importing the same module (in different Powershell sessions) is because I'm currently writing a cmdlet using C#, and I start a powershell instance as part of debugging the cmdlet.
The Import-Module command gives me no feedback whatsoever of whether or not the loading succeeded.
PS C:\Source\MyProject> Import-Module -Name "./MyAssembly.dll"
PS C:\Source\MyProject> Get-Stuffz
Get-Stuffz : The term 'Get-Stuffz' is not recognized as the name of a cmdlet, function, script file, or ope
rable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again
.
At line:1 char:1
+ Get-Stuffz
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-Stuffz:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Any help or suggestions are welcome.

Get-QADUser' is not recognized as the name of a cmdlet

I am trying to take users from AD with some filter criteria but while executing Get-QADUser' command I am getting following error.
Get-QADUser : The term 'Get-QADUser' 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 line:1 char:1
+ Get-QADUser
+ ~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-QADUser:String) [], CommandNotFoundExce
ption
+ FullyQualifiedErrorId : CommandNotFoundException
Can Anybody tel me how to resolve this?
I using following reference link
http://wiki.powergui.org/index.php/Get-QADUser
QAD cmdlets FAQ
To load QAD cmdlets for one particular script:
Add this line in the beginning of the script:
Add-PSSnapin Quest.ActiveRoles.ADManagement -ErrorAction Stop
To make QAD cmdlets available for all your scripts on a particular computer:
Open your PowerShell profile (for example, by starting PowerGUI Script Editor and selecting File / Open Current User Profile). Add this line to the profile code:
Add-PSSnapin Quest.ActiveRoles.ADManagement -ErrorAction Stop
PS. Original Quest article was recommending -ErrorAction SilentlyContinue but I cannot agree with such.