Equivalent of "script" command in Windows Shellscript? - windows

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.

Related

Powershell Core to read file metadata

I need a way to read file metadata using Powershell Core 7.x on macOS.
In a MS Windows environment, I was able to use Shell.Application COM object and getDetailsOf() method to retrieve the information. However, I can't find this option in PowerShell Core on macOS.
I found someone who used external commands (I guess some are written in python) in PowerShell core to retrieve the information, but I would like to do that using PowerShell only.
Does anyone know if this is possible with Powershell core?
Thanks
Instead of using a "Shell.Application COM object" to execute an external tool, you can just use the Start-Process cmdlet and capture the output of this process:
$f = New-TemporaryFile
Start-Process exiftool -ArgumentList "myImage.jpg" -RedirectStandardOutput $f.FullName -Wait
$result = Get-Content -Path $f.FullName
Remove-Item -Path $f.FullName
# Now, the result of the exiftool is available in $result for further processing
Shell.Application is part of Win32, it's literally the Windows shell!
So when you load it via COM, you're basically telling Powershell do the same function the shell uses for the UI details tab, GetDetailsof. COM is just how you're communicating to the process, to tell it to run a particular method.
So could this work on Powershell Core? Only if you're running Powershell Core on a Windows system. That is because the dependency is a method provided by the platform. You have no other choice than to read the metadata yourself, via another library.
You might want to check out, metadata-extractor-dotnet
Something like that is what you'd need to implement the functionality. I don't have experiance using that lib myself, but at least it might provide a good jump off point for further research.

command prompt auto write to log file

I have a batch file named very_good02.bat. When I run it, it will show some progress info in command prompt.
I wish to auto write a log file that consists of all this progress information with the file name I keyed (which is very_good02).
I tried to ran it like this : very_good02.bat > very_good02log.txt the process is running in background where I can't see them in command prompt.
If I understand correctly, what you are seeking is a way to "tee" the pipeline to both the console and a log file.
The tee command has been in UNIX/Linux for a long time. No such thing in Windows cmd.exe. But, PowerShell does have Tee-Object. At a PowerShell command prompt, use help Tee-Object -full for more information.
powershell -NoLogo -NoProfile "& .\very_good02.bat | Tee-Object -FilePath 'C:\src\t\very_good02.log'"
Naturally, this is easier if your whole script is in PowerShell and not in cmd script language. You don't have to use PowerShell, but that is the clearly stated direction from Microsoft.
I am reminded that this can, actually, be done in a cmd .bat file script. Not easily, but it can be done. https://www.dostips.com/forum/viewtopic.php?p=32615#p32615 If anyone can do it in a .bat file script, Mr. Benham can.

Convert localized path to the English

if user installs windows in his native language(not english) he can access files using localized paths ie. insted C:\Users\UserName\Desktop he can access Desktop using C:\LocalizedUsersName\LocalizedDesktopName Is there any Powershell command which can convert/translate local version of the Path to the English version?
There is nothing specifically for a path, yet, basically, along the same lines as you'd do this for other strings you are working with. See the discussions, examples, and answers below.
International Settings Cmdlets in Windows PowerShell
Using the Microsoft Translator API from PowerShell
Localization and PowerShell
plattsoft_PSUICultureExample
Forcing PowerShell errors output in English on localized systems
You can change the pipeline thread's CurrrentUICulture like so:
[Threading.Thread]::CurrentThread.CurrentUICulture = 'fr-FR'; Get-Help
Get-Process
I'm on an English system but before I executed the line above, I
updated help like so: Update-Help -UICulture fr-FR
$ENUS="[Threading.Thread]::CurrentThread.CurrentUICulture = 'en-US'"
then invoke-expression $ENUS; Get-Help Get-Process "invoke-expression
$ENUS;" is little shorter if you need to run many commands

export powershell cmdlet as executable

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

How can I view a log file from powershell console ? (i.e. powershell equivalent of 'less')

What is the powershell equivalent of 'less'?
I see 'more', but it lacks some of the features I rely on (e.g. searching through the file)
I seek a pager (equivalent of 'less') which allows searching (match or ignore case), multiple files at once, etc.
Some of our servers run windows 2008 and I lack admin privileges to install cygwin
I had heard windows 2008, MSFT got their act together and provided some easy-for-admins tools.
Update:
I should give some context:
I know little about power shell
New servers have 2008 on them
While I affection for many tools of yore, the dos prompt is not one of them
I was hoping that Powershell had the equivalent of grep,ls,less, xargs, et
I understood that powershell gave us those tools
I fired off my question quickly.
thanks
It reads like you know you can do this:
gc logfile.log | more
(GC is an alias for Get-Content).
You may be able to do the filtering etc.. with this more information can be found by running these commands:
Get-Help Get-Content Get-Help
Get-Content -Examples
(Get-Help gc would work fine as well).
And the bits you may be interested in are limit\filter etc...
Get-Help gc -Parameter * | more
I just use the GOW version of less, works fine.
I don't know of any direct analogue for less in powershell that you can implement easily. Your best bet is to get a windows implementation of less that is outside of cygwin, that way you can just drop in the binary somewhere accessible to your account.
to get grep/vim/wget and other Linux like commands in powershell I suggest running.
iex (new-object net.webclient).downloadstring(‘https://get.scoop.sh’)
then
scoop install grep
scoop install perl
scoop install vim
and to get a list of all of them
scoop search
In Windows 10 PowerShell + Cygwin I use:
gc .\myfile.log | less
Previously I was trying to use cygwin directly:
less .\myfile.log
but it shows binary file because of invalid charset setting between 32b-bit and 64-bit.
I was hoping that Powershell had the equivalent of grep,ls,less, xargs, et
In the case you missed this question (top voted) you might enjoy this answer.

Resources