export powershell cmdlet as executable - windows

Are windows power shell cmd lets stored on the computer as individual files or not.
If so what is the extension of those files.
If not is there a way to export them as individual files?
this would be useful say if I only needed a few cmdlets I could just have them as individual files and invoke them by typing the file name.

Cmdlets cannot be exported as executable commands and moved to other systems. They require the PowerShell engine in order to operate and are not self-contained.

You can use proxy command and export this cmdlet as function

Cmdlets reside in these DLLs so there is no exe and I don't believe individual Cmdlets can be exported to another system.
PS > (Get-Command Write-Host).DLL
C:\windows\Microsoft.Net\assembly\GAC_MSIL\Microsoft.PowerShell.Commands.Utility\v4.0_3.0.0.0__31bf3856ad364e35\Microsoft.PowerShell.Commands.Utility.dll
PS > (Get-Command Get-ChildItem).DLL
C:\windows\Microsoft.Net\assembly\GAC_MSIL\Microsoft.PowerShell.Commands.Management\v4.0_3.0.0.0__31bf3856ad364e35\Microsoft.PowerShell.Commands.Management.dll

Related

how to make a Powershell script avaiable from anywhere

I have a Powershell script I would like to make "public", meaning I want to be able to execute the script from any folder as you can do from the command prompt.
Is this possible?
You can also add an alias in your local powershell profile
Alias Example in profile
Set-Alias hello C:\scriptlocation\script.ps1
Now anytime you type hello, the script.ps1 will run.
More info on the various profiles that the alias can be saved to.
https://devblogs.microsoft.com/scripting/understanding-the-six-powershell-profiles/
You can explore the use of your powershell profile to achieve this. See: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles.
If the script is just a function or some variables, you can copy and paste the content into your profile.
Alternatively, if the script represents a standalone unit of code you want to keep separate, you could import it into your main profile as such:
get-content -path C:\blahblahblah\scriptName.ps1 -raw | invoke-expression
Finally, if you are writing an "advanced" powershell function, or are trying to do things officially, you could investigate the use of powershell modules as a way to export your function. See: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_modules
#Lee_Dailey's answer of adding the script to your path is also viable. If you want to add a lot of scripts, one way to do that is to add a folder like C:/PowershellScripts/, to your path, save your scripts there, and then you'll be able to invoke your .PS1 file from anywhere.
Name the script something meaningful ie: awesome.bat Save it in a dir and add the dir to windows env. Command awesome will be globally available.

Running VS 2017 Build Tools in Windows PowerShell

I dislike IDEs, so I installed the VS 2017 Build Tools so I can work via command-line.
The install went fine, and everything works out of Windows CMD, however, PowerShell is much better, and I prefer to use PS. The issue here is that according to MSDN:
The Visual C++ command-line tools use the PATH, TMP, INCLUDE, LIB, and LIBPATH environment variables, and may also use tool-specific environment variables. Because the values of these environment variables are specific to your installation, and can be changed by product updates or upgrades, we recommend that you use vcvarsall.bat or a Developer Command Prompt shortcut instead of setting them yourself. For information about the specific environment variables used by the compiler and linker, see CL Environment Variables and LINK Environment Variables.
I shouldn't set the Environment Variables myself, and that's fine with me, the only issue is that when I run the vcvarsall.bat in PS, no environment variables change. I am new to PS, so I'm guessing that .bat files can't alter session environment variables. If that's the case, then I can't work out of PS. As a side note, the CL and LINK variables never show up, I'll explain below.
I figured I should find out what the variables are. I echoed all my variables to a text file before and after running the batch file, and wrote a short Java program to find anything new, or modified. These are them. As you can see the CL and LINK variables are not present.
How do I solve this issue? I was thinking of writing my own batch file, but if the first one didn't work, why would mine? I didn't see anything on the attached MSDN page, or any links there about how to make this work for PowerShell.
Write a batch file that 1) invokes vcvarsall.bat, and 2) invokes PowerShell, like so (this one is specific to VS 2015):
#CALL "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" %*
#start powershell
%* allows us to pass the same arguments to this file as you would to vcvarsall.bat.
PowerShell will then run with the environment block prepared for it. The other way around doesn't work because PowerShell doesn't execute batch files itself -- it relies on cmd to do that, and as a child process, that has its own environment block that doesn't reflect on its parent.
<#
.SYNOPSIS
Invokes the specified batch file and retains any environment variable changes it makes.
.DESCRIPTION
Invoke the specified batch file (and parameters), but also propagate any
environment variable changes back to the PowerShell environment that
called it.
.PARAMETER Path
Path to a .bat or .cmd file.
.PARAMETER Parameters
Parameters to pass to the batch file.
.EXAMPLE
C:\PS> Invoke-BatchFile "$env:ProgramFiles\Microsoft Visual Studio 9.0\VC\vcvarsall.bat"
Invokes the vcvarsall.bat file. All environment variable changes it makes will be
propagated to the current PowerShell session.
.NOTES
Author: Lee Holmes
#>
function Invoke-BatchFile
{
param([string]$Path, [string]$Parameters)
$tempFile = [IO.Path]::GetTempFileName()
## Store the output of cmd.exe. We also ask cmd.exe to output
## the environment table after the batch file completes
cmd.exe /c " `"$Path`" $Parameters && set " > $tempFile
## Go through the environment variables in the temp file.
## For each of them, set the variable in our local environment.
Get-Content $tempFile | Foreach-Object {
if ($_ -match "^(.*?)=(.*)$") {
Set-Content "env:\$($matches[1])" $matches[2]
}
else {
$_
}
}
Remove-Item $tempFile
}
$VcVars = 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Auxiliary\Build\vcvarsall.bat'
Invoke-BatchFile $VcVars x64
cl hello_world.cpp

How do I find the path to the .exe file that created a process?

Preferably a command line tool to do this...
I understand that there is a program called tasklist.exe in Windows systems, and it has many powerful features to view processes currently running on the system.
Unfortunately it does not have the functionality to view the path of the .exe file that created or spawned the process in the first place.
I finally decided to take a tour of my system and I've noticed some shady looking processes and I want to check if they live in equally shady looking places on my system.
Any ideas?
You can use PowerShell.
Click Start -> Run, and type powershell to invoke Power Shell.
View all processes currently:
tasklist
Show full path of .exe file (Example command for Notepad++):
Get-Process notepad++ | Select-Object Path
You will see output:
Path
C:\Program Files (x86)\Notepad++\notepad++.exe

Equivalent of "script" command in Windows Shellscript?

There is script command on GNU/Linux machine which allows to capture all command line activity into a file . This is really helpful tool especially when we learn something new and we want to save the commands and their output for future reference.
I am currently learning the Git on Windows PowerShell terminal and I wanted to capture all the commands and their output in a file for future reference.
Is there any way/command do achieve it on Windows PowerShell?
Try with Start-Transcript and Stop-Transcript cmdlet.
You can also use Start-Transcript for ISE Editor module because these CmdLet don't work natively in ISE.
It exists standard CmdLets : Start-Transcript and Stop-Transcript.
You'll find more informations about these CmdLet in the associeted TechNet documentation.

Running programs easily from a command line on Windows

Linux allows me to have a short system path by placing binaries in just a few locations. I don't have to edit the path because I just installed a new application, and I don't have to hunt for applications I want to run. How can I, with PowerShell as the program I use to launch programs from, accomplish the same thing on Windows (Vista)?
Windows Vista has symlinks now via mklink.
Perhaps you could setup a "C:\bin" folder and generate symlinks to point back to the original binaries. That is assuming that Windows Vista's symlinks work similarly to the ones in Linux. Here's a short tutorial.
Many programs create an application paths entry in the registry (HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths). For those applications, you can start them like so:
PS> Start-Process <application name>
PS> Start-Process excel
If you don't have PowerShell V2, which provides Start-Process, you can use the PowerShell Community Extensions on V1.
It sounds like adding a few directories to your path environmental variable might help. From the command prompt you can view all environmental variables with the set command. Then you can cut and paste your path and use set again to add to it.
If you prefer the GUI route, right click on My Computer → Properties → (in Windows Vista and Windows 7 go to "Advanced System Settings" on the left. In Windows XP, skip this step) → Advanced Tab → At the bottom there is an Environmental Variables button.
When something is invoked from the command line, Windows checks in all the directories marked in the path first. After your application directory is in the path, you can execute it without fully qualifying your path.
You could always add a .cmd file as an alias.
I install applications into C:\bin.
Using specifically PowerShell you can just create aliases for programs you want to start. I doubt that this is actually less work than editing the PATH environment variable, though.

Resources