PowerShell Cmdlets, Get-________ code line modification to get output in Text file

User avatar
DocAElstein
4StarLounger
Posts: 545
Joined: 18 Jan 2022, 15:59
Location: Re-routing rivers, in Hof, Beautiful Bavaria

PowerShell Cmdlets, Get-________ code line modification to get output in Text file

Post by DocAElstein »

Hi
The TLDR version
These three PowerShell Cmdlets all give me a similar long list
Get-Service
Get-Service|Format-List
Get-Service|Format-List|Format-Table


Does anyone know how to modify those to give the output in a text file?


_.____________________________________

The full story.
Its related to this Thread… https://eileenslounge.com/viewtopic.php ... 53#p291953
DocAElstein wrote:
22 Jan 2022, 14:01
...Ideally a list with that Dienstname along with something else like the description or the Name )
I am trying to learn PowerShell.... I know there is ways to Get a list through “piping them out" or some such, but I haven’t got that far yet. Ideally a list with that Dienstname along something else like the description or the Name )...
I am not quite there yet on this one, but I am on the way….. actually it looks not that difficult….

Just a simple code line in PowerShell

Get-Service
ImageImage

But according to what I have read, that what you see in the PowerShell window may be misleading. It might be some truncation of the info that is there. Sure enough we get more, rather than less as we are “going down the line” as it were

Get-Service|Format-List
Image


Get-Service|Format-List|Format-Table
Image


To investigate this further I would like to both get and compare what all those 3 Cmdlet lines give me: it might be exactly what you see in the blue PowerShell window. But I would like to know for sure

Does anyone know the next step to get those list in a text file? I think I have seen it done in the simpler black cmd window thing. I have not had any luck yet goggling how to do it in the blue PowerShell window

I could then use VBA to import and find stuff in that list.
( I know that is probably too much effort for a single requirement, but for finding things in the future it might be useful as I will be doing some similar stuff where getting the full text file output could be useful)

Thx
Alan
I seriously don’t ever try to annoy. Maybe I am just the kid that missed being told about the King’s new magic suit, :(

User avatar
HansV
Administrator
Posts: 78236
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: PowerShell Cmdlets, Get-________ code line modification to get output in Text file

Post by HansV »

Best wishes,
Hans

User avatar
DocAElstein
4StarLounger
Posts: 545
Joined: 18 Jan 2022, 15:59
Location: Re-routing rivers, in Hof, Beautiful Bavaria

Re: PowerShell Cmdlets, Get-________ code line modification to get output in Text file

Post by DocAElstein »

Thanks Hans for about the redirect to the Redirect info :)

I think I have the approximate almost answer, at least to get something to a text file. Not sure if I got it from that link or experimenting. - A combination of both, probably. That article is typical of most Microsoft help. You need to know the answer to know how to see the answer at their help on it. Often Microsoft help reminds me of the rough notes I make myself to jog my memory on something after I have fully understood it.
To learn from, Microsoft help is usually useless.
Anyway I got a bit further now, and I expect that link may have helped a bit. I expect it’s one of those articles I will go back to many times later and think.. “..Ah, I see now what they were trying to say…. “

The very simplified, shortened answer for now is like

Get-Service > test.txt

It’s a bit more complicated then that, but I am smashing my head at a brick wall at the moment trying to get the format as I want. I may need to ask for related help on that if I don’t get there myself…

Thanks again for that link. It will probably prove to be more useful when I get further with the larger problem that this is related to.

Alan
I seriously don’t ever try to annoy. Maybe I am just the kid that missed being told about the King’s new magic suit, :(

JoeP
SilverLounger
Posts: 2051
Joined: 25 Jan 2010, 02:12

Re: PowerShell Cmdlets, Get-________ code line modification to get output in Text file

Post by JoeP »

What is the format you want? You can do a lot in PS to format the output.
Joe

User avatar
DocAElstein
4StarLounger
Posts: 545
Joined: 18 Jan 2022, 15:59
Location: Re-routing rivers, in Hof, Beautiful Bavaria

Re: PowerShell Cmdlets, Get-________ code line modification to get output in Text file

Post by DocAElstein »

Hi Joe
I have spent a day trying to get three things in a simple three column list, or table, (in a text file)

I have tried hundreds of different versions of a code line like this, as well as other more complicated code lines.

Get-Service|format-table name,displayname,StartType > test.txt

So far I can get one or two columns but the third is never there.

Image

Somehow something is truncating the data I want.
I could do three separate code lines to get each of those three Property things, name,displayname,StartType , in a separate file.
So like this would get me what I want in three separate files

Get-Service|format-table StartType > test1.txt

Get-Service|format-table name > test2.txt

Get-Service|format-table displayname > test3.txt

But it worries me to not know why I can’t get all three columns at once , since what ever I don’t understand that is truncating the data today in trying to get all three things at once, might rear its ugly head another day and truncate something in one of those three separate lists. Also if I have all three at once, in the same list or table, then I am more sure , or assume I can be more sure, that the order of each column is the same. Otherwise I must assume that all three lists have the properties in the same order. I suppose they should all come out in the same order. But maybe something m,ight happen in between getting each of the three list that changes the order that they are given ( “streamed out” ) in

_.__________________________________________________________

Thinking about this again in a slightly different direction, or more precisely different orientation, maybe it would be safer to get a list like this
Get-Service|format-list name,displayname,StartType > test.txt
So far that seems to work to give me all the info I want.

Image

I can easily manipulate that text file with VBA to get it in any format I want, including a three column table, But organised like that initially, there is perhaps more certainty that the three properties belong together

I would still like to understand why I can’t get a simple three column table directly using PowerShell script
I seriously don’t ever try to annoy. Maybe I am just the kid that missed being told about the King’s new magic suit, :(

JoeP
SilverLounger
Posts: 2051
Joined: 25 Jan 2010, 02:12

Re: PowerShell Cmdlets, Get-________ code line modification to get output in Text file

Post by JoeP »

What version of PS are you using?

The help for Get-service says "Beginning in PowerShell 6.0, the following properties are added to the ServiceController objects: UserName, Description, DelayedAutoStart, BinaryPathName, and StartupType."
Joe

User avatar
DocAElstein
4StarLounger
Posts: 545
Joined: 18 Jan 2022, 15:59
Location: Re-routing rivers, in Hof, Beautiful Bavaria

Re: PowerShell Cmdlets, Get-________ code line modification to get output in Text file

Post by DocAElstein »

I think I mostly have PowerShell 5.1
I can get the information I want,
For example the last List screenshot, ( I may have edited as you posted ), has all the data in one text file
( although I expect DelayedAutoStart is not there for reasons we discussed previously – I expect it is shown as AutoStart – but that is not directly the issue here ).

I just don’t understand why I can’t get that data in a simple three column table directly using PS.

_.________________________________

(By the way, I am surprised that in version 6.0 they introduce StartupType , since I assume that is the property StartType which I am seeing, ( https://i.postimg.cc/yxbQx6nq/Start-Type.jpg ) Presumably they are the same ? )
I seriously don’t ever try to annoy. Maybe I am just the kid that missed being told about the King’s new magic suit, :(

JoeP
SilverLounger
Posts: 2051
Joined: 25 Jan 2010, 02:12

Re: PowerShell Cmdlets, Get-________ code line modification to get output in Text file

Post by JoeP »

I'm not sure what it going on but the only way I can get the starttype to display is if it is before the displayname.

Will starttype, name, displayname work for you?
Joe

JoeP
SilverLounger
Posts: 2051
Joined: 25 Jan 2010, 02:12

Re: PowerShell Cmdlets, Get-________ code line modification to get output in Text file

Post by JoeP »

BTW, you need to use startuptype in PS 6 or later to get Automatic Delayed to display. If you use starttype the type is display as Automatic even for those that are delayed.
Joe

User avatar
DocAElstein
4StarLounger
Posts: 545
Joined: 18 Jan 2022, 15:59
Location: Re-routing rivers, in Hof, Beautiful Bavaria

Re: PowerShell Cmdlets, Get-________ code line modification to get output in Text file

Post by DocAElstein »

Thanks for trying, Joe
JoeP wrote:
31 Jan 2022, 16:41
Will starttype, name, displayname work for you?
Strangely it does. https://i.postimg.cc/X7JsT5Ls/starttype ... ayname.jpg
( I thought I had tried most combinations. I must have missed that one.)

Until I can figure out why it doesn’t work in a different order, I am reluctant to go with the format-Table solution, in case its some strange text dependant truncation thing that might for some reason suddenly occur again in the starttype, name, displayname order.
_.________________________________________________-
JoeP wrote:
31 Jan 2022, 16:44
you need to use startuptype in PS 6 or later to get Automatic Delayed to display. If you use starttype the type is display as Automatic even for those that are delayed.
Thanks, that’s good to know. I suppose it makes some sense. - Useful to know when making my script do the best possible in whatever version it is run on
I seriously don’t ever try to annoy. Maybe I am just the kid that missed being told about the King’s new magic suit, :(

JoeP
SilverLounger
Posts: 2051
Joined: 25 Jan 2010, 02:12

Re: PowerShell Cmdlets, Get-________ code line modification to get output in Text file

Post by JoeP »

FWIW, I doubt Microsoft is going to be putting much effort into Windows PowerShell. I believe the effort will go towards the open-source PowerShell. Cant' say for sure but I think Windows PS will disappear at some point in the not too distant future.
Joe

User avatar
DocAElstein
4StarLounger
Posts: 545
Joined: 18 Jan 2022, 15:59
Location: Re-routing rivers, in Hof, Beautiful Bavaria

Re: PowerShell Cmdlets, Get-________ code line modification to get output in Text file

Post by DocAElstein »

JoeP wrote:
31 Jan 2022, 17:07
FWIW, I doubt Microsoft is going to be putting much effort into Windows PowerShell. I believe the effort will go towards the open-source PowerShell. Cant' say for sure but I think Windows PS will disappear at some point in the not too distant future.
I hadn’t noticed that.
At the moment I am only interested in a .ps1 file running somehow.
I have been running it in a PowerShell window as I had only seen these things running like that. I just assumed that was how one does these things.

Something new for me: Only since I noticed it, in the last few days, I have been finding the PowerShell ISE development window very useful to run short bits of script to test it out on before then incorporating things into the main full long script. That is how I have noticed errors in some people’s script that I am learning from and taking parts from.

I am very new to Power… Shell, script, or whatever it should be referred to ..
I am fairly confident with VBA, but know very little else at all in computing.
It seems that with PowerShell script is similar, but with a couple of differences, to VBA (script).
_1 It has an extra parallel concept, and equally vague, to the OOP, Object Orientated Programming, concept, which it also has.
This extra imprecisely defined concept is some Piping idea where in code lines at a pipe | you have a lot of stuff there which you can then narrow down and pick out bits. To a Layman there is no difference to the Hierarchical Object – Property idea in OOP. (Possibly you have to be a Professional to understand the difference.)
Something to do with piping streams about. I can understand the concept. In the grass fields next to the disused railway track I jog along, - in a lot of places streams vanish in piping and irrigation tracks. It seems to organise things in a sensible way, and does not spoil the nature so much. Especially with a light snow coating, its all very beautiful this time of year.
_2 The coding is a lot less likely to error by typos either at the writing stage or in running. It can error in a similar way to VBA sometimes, but its less likely to: Half the time it responds to errors, the rest of the time it responds to errors as VBA does if you use On Error Resume Next

The people whose codes I have been looking at and learning from and taking things from ( and finding a lot of errors in ) , don’t seem to use the PowerShell ISE development window, and instead spend a lot of their time at GitHub, which seems to be a platform designed to sabotage a script development as it totally screws up any control of what is going on.
As PowerShell Script does not error so much when things go wrong, GitHub is a good place to develop a script if you want it to fail, since things are just merged and run and if the code does not error it’s assumed the code change or addition worked.
I expect I might be less critical if I understood all about GitHub.
I am trying to learn about that GitHub.as well, but I am concentrating on learning and getting the coding to work first.
I am personally finding it easier just to try and sift through all the various versions people have or have suggested at GitHub for any project, and then start again from scratch with my own new coding.
People seem to go around in circles at GutHub putting things in that fall out somewhere or duplicating things leading to redundant coding or worse introducing script error from typos and things that go unnoticed.
GitHub sounds like a great idea. But it’s not working for the PowerShell coding and projects that I have been looking at.
Perhaps its better for other scripts and languages. I don’t know
( I think I heard its owned by Microsoft? … )
I seriously don’t ever try to annoy. Maybe I am just the kid that missed being told about the King’s new magic suit, :(

JoeP
SilverLounger
Posts: 2051
Joined: 25 Jan 2010, 02:12

Re: PowerShell Cmdlets, Get-________ code line modification to get output in Text file

Post by JoeP »

There is a plethora of information at PowerShell Documentation including a link to a PowerShell script Gallery. That gallery has tons of useful stuff.

You can spend immense amounts of time at that site.
Joe

User avatar
DocAElstein
4StarLounger
Posts: 545
Joined: 18 Jan 2022, 15:59
Location: Re-routing rivers, in Hof, Beautiful Bavaria

Re: PowerShell Cmdlets, Get-________ code line modification to get output in Text file

Post by DocAElstein »

JoeP wrote:
31 Jan 2022, 19:59
...You can spend immense amounts of time at that site.
But that would be bad,. … wouldn’t it? – I wouldn’t have as much time to be here! ……. :)
But seriously I will take a look. It can’t be any worse than taking script from GitHub, hopefully the stuff there has a better success rate in the coding actually working and doing half way what it’s intended to do. There may be some relation to stuff there and some “module” thing I “invoked” a while back to usefully turn a PowerShell script into an executable .exe thing

The general article and Blog site information on the internet that I am finding continually seems to be very bad – I don’t doubt some of the people writing the stuff know what they are talking about but almost all seem to be obfuscating it behind what they write

On YouTube, as most usual, you need a lot of time to wade through the 95% of rubbish to find the 5% that is good or OK. This guy is OK in places, but you need to be fluent in broken Swahili-Hindi-English to understand him. https://www.youtube.com/watch?v=FVs42IU ... j-N&t=720s

_._______________________________________________________________________________________-

I have got a bit further , by about 80% experiment, 20% internet research. Of the 20%, most of it was trying to understand the very fundamental theory of Piping and similar things then starting from scratch again myself
I have figured out what/ where the strange truncating problem is coming from.
So now I can get consistently the three column table
, and am close to understanding all about how to do it. I probably need just a bit longer to get a 100% understanding of the syntax.- I am almost there.

_1 Coming back to the very simplified, shortened code lies of this sort of form: Truncation Oddity
Get-Service > test.txt # Output to text file
Get-Service|Format-Table name,displayname,starttype > test.txt # Output to text file
Get-Service|Format-Table name,displayname,starttype 1> test.txt # Output to text file
Get-Service|Format-Table name,displayname,starttype # output in blue console window

I can consistently get now a simple three column table from those sort of lines. … How? :-
My experiments, and what I picked up from research are telling me that the width you end up with for the final output is related to the width of the window PowerShell window from which you run the code line.
That goes for the width you end up with, ( in other words if/what gets truncated), both in
_ the direct blue window console output
And
_ the success stream redirected stuff, > , that finds its way in the final text file.
In simple terms, the answer is this: make the PowerShell window very wide, and your text file ( or console output ) will have all the columns.
I am not sure yet of the exact correlation between the width of the console and width of “truncation” in the console and the final text file: Sometime it seems to tie up very well ;Other times it may be you see all columns in one but not in the other. But if the PowerShell window is very wide, then it’s a sure bet you will see all 3 columns in both.
Image

But that is obviously a bit of a fiddle and maybe not always a practical solution

_2 Doing it properly, practical Solution
The final sensible solution lies in using the full syntax.
( the variations of the > are something of a shorthand, - not really a shorthand or Alias, - I expect possibly they are some combination of a shorthand and an attempt at backward compatibility to the black command, cmd, window stuff )

I am not 100% there yet, mostly because I am not fully sure I understand the syntax yet, but this is close to the sort of syntaxes that will get consistently a 3 column table like output in a text file, regardless of your PowerShell window width.

Code: Select all

 Get-Service|Format-Table -AutoSize  name,displayname,starttype| Out-File -FilePath test.txt -Width 1000 
Get-Service|Format-Table -AutoSize  name,displayname,starttype| Out-File -FilePath 'C:\Users\Elston\Desktop\test.txt' -Width 1000 
It looks very beautiful in the PowerShell ISE window
Get-Service|Format-Table -AutoSize name,displayname,starttype| Out-File -FilePath test.txt -Width 1000


I have not seen the -AutoSize make any difference yet. But some people suggested you needed it, so until I know better I am including it.
I set the figure 1000 arbitrarily after I found
_ that a minimum figure of between about 120 and 160 was needed to get me all the columns in my current example
, and
_ I saw no ill effects from using an extremely large number.
So as I don’t 100% fully understand what’s going on yet, I guessed a fairly big number would be OK.

Notes:
_(i) It is very important to liberally use the Clear command and keep your eye on if a scroll bar appears on the blue console when doing any experiments: This is because the “effective” PowerShell console width increases when you have a scroll bar. The scroll bar is somehow automatic, I think. The scroll bar can, for example be somehow “invoked” be a previous long ( wide ) code line.
The point I am making here is that if a scroll bar is present that lets you scroll a long way to the right, then likely the simplified code line of like Get-Service|Format-Table name,displayname,starttype > test.txt will very likely get all your columns in the text file even if the window you have in front of you is very narrow.
Image

_(ii) When checking out your text file during any experiments, make sure you always scroll across to the right, as the last column may initially be hidden across to the right when you open up the text file. ( In other words the text file may open with a scroll bar, and to see the last column you may need to scroll to the right )


Alan
Last edited by DocAElstein on 01 Feb 2022, 16:28, edited 6 times in total.
I seriously don’t ever try to annoy. Maybe I am just the kid that missed being told about the King’s new magic suit, :(

JoeP
SilverLounger
Posts: 2051
Joined: 25 Jan 2010, 02:12

Re: PowerShell Cmdlets, Get-________ code line modification to get output in Text file

Post by JoeP »

Check out Getting started with PowerShell. Videos by people who really know PS.

Another good set - Advanced tools and scripting.
Joe

User avatar
DocAElstein
4StarLounger
Posts: 545
Joined: 18 Jan 2022, 15:59
Location: Re-routing rivers, in Hof, Beautiful Bavaria

Re: PowerShell Cmdlets, Get-________ code line modification to get output in Text file

Post by DocAElstein »

Hi
I found another syntax that seems to work

Code: Select all

 Get-Service|Select-Object name,displayname,starttype|Format-Table -AutoSize|Out-File -FilePath 'C:\Users\acer\Desktop\test.txt' -Width 1000 
It just came about by chance as I was learning a bit more PowerShell script coding.
It looks like it could be a bit nearer to the real “proper” syntax.
Learning PowerShell scripting coding isn’t turning out to be as difficult as I thought it would be. Some ideas and concepts don’t seem that different from VBA. (I am also quite encouraged by the very beautiful colours the coding has when pasted in the Notepad++ text editor or the ISE development window)

Get-Service|Select-Object name,displayname,starttype|Format-Table -AutoSize|Out-File -FilePath 'C:\Users\acer\Desktop\test.txt' -Width 1000

I find the ISE development window is also very nice to work in. I am not sure why a lot of the people whose codes I have been looking at don’t seem to have used it, or even discovered it. Possibly because their background is often in System Administration and they are less so interested in programming?
Their testing of making additions to existing large coding seem to comprise of running the entire code and if it doesn’t error they think all is well. That’s dangerous as PowerShell is less likely to error than VBA when things don’t work. I am finding an awful lot of careless errors introduced when additions were added, and these went unnoticed. (It doesn’t help, IMO, that they use GitHub for coding development, which seems to have been a disaster for their code development. The simplest change seems to involve a complicated process that some of them haven’t mastered yet, and they screw up and end up testing a different script version to the one they made the change to!!!)

The ISE development window seems very similar to the VB Editor, but doesn’t seem to have quite as many useful tools, or maybe I just haven’t found them yet.

Next step for me is to install 7.2.
Based on some of the things noted in this, and some related, Threads, I think I might like to investigate a script that runs initially from the 5.1, somehow then downloads and installs the 7.2, or has at least an option to do that, and then has the possibility to run the rest of the script from that.


Alan

( Edit, P.S. I have automated getting that list out into Excel with PS 5.1
https://excelfox.com/forum/showthread.p ... #post12776
https://excelfox.com/forum/showthread.p ... #post15357

I hope to be able to do the same with PS 7.2 )
Last edited by DocAElstein on 10 Feb 2022, 23:08, edited 3 times in total.
I seriously don’t ever try to annoy. Maybe I am just the kid that missed being told about the King’s new magic suit, :(

JoeP
SilverLounger
Posts: 2051
Joined: 25 Jan 2010, 02:12

Re: PowerShell Cmdlets, Get-________ code line modification to get output in Text file

Post by JoeP »

Get-Service|Select-Object name,displayname,starttype|Format-Table -AutoSize|Out-File -FilePath 'C:\Users\username\Downloads\test.txt' -Width 1000

runs successfully on my Win11 Pro PC with PS 7.2.1
Joe

User avatar
DocAElstein
4StarLounger
Posts: 545
Joined: 18 Jan 2022, 15:59
Location: Re-routing rivers, in Hof, Beautiful Bavaria

Re: PowerShell Cmdlets, Get-________ code line modification to get output in Text file

Post by DocAElstein »

JoeP wrote:
09 Feb 2022, 16:45
Get-Service|Selec.....runs successfully on my Win11 Pro PC with PS 7.2.1
Thx Joe, good to know.
_.______________________________________-

Joe, ( and or anyone else), … How do you run PowerShell 7? ( 7.2)
I ask because , I goggled and found some forum answers from a year ago with an insane number of views, and they seem to go about it in a slightly round about way, or I am just too stupid to understand what they were doing…

By the way, I tried this simple way, and initially it seems OK:
_ I downloaded PowerShell 7 first:- ( I used a button on a PowerShell GUI, but that was just for convenience as I am interested in doing that for my current activities .
The important code line that worked, sort of, after a fashion, was winget install --id Microsoft.PowerShell --source winget | Out-Host

_ I found the folder it looks to have been put in, double clicked on something that looked like an .exe file, https://i.postimg.cc/YSV4y7HR/pwsh-in-C ... wnload.jpg
, and up popped a black window:
https://i.postimg.cc/0QwzT45s/Doble-cli ... ck-box.jpg
https://i.postimg.cc/mrhzwD0h/black-box ... ll-7-2.jpg
black box PowerShell 7.2.JPG

_ I went on and tried a few things and it seemed to be doing PowerShell stuff.
Example: I confirmed what you said about the different syntaxes of startuptype and starttype
I also went on to try the stuff related to the other thread, and confirmed as you did , that I can set the option of Automatic (Delayed Start) provided I use something like this in PS 7
Set-Service "dmwappushservice" –StartupType AutomaticDelayedStart
( Sometimes I had to launch the black window a bit differently – instead of double clicking on the pwsh exe thing, I had to right click on it, and select launch as Administrator ( https://i.postimg.cc/bvYJpbrG/Right-Cli ... trator.jpg ) That makes sense, - I often have to do the similar run as Admin stuff in PS5 or when using the ISE Development window )[/color]

One reason I ask about how others run PS7 is that I have not figured out yet how to run it from VBA, ( PS5 was quite easy to do in VBA ) and so I am wondering if I need to think about it in a totally different way, since its not a windows thing, maybe…

Alan
You do not have the required permissions to view the files attached to this post.
Last edited by DocAElstein on 16 Feb 2022, 16:27, edited 1 time in total.
I seriously don’t ever try to annoy. Maybe I am just the kid that missed being told about the King’s new magic suit, :(

JoeP
SilverLounger
Posts: 2051
Joined: 25 Jan 2010, 02:12

Re: PowerShell Cmdlets, Get-________ code line modification to get output in Text file

Post by JoeP »

In the VBA script try using PWSH instead of PowerShell.
Joe

User avatar
DocAElstein
4StarLounger
Posts: 545
Joined: 18 Jan 2022, 15:59
Location: Re-routing rivers, in Hof, Beautiful Bavaria

Re: PowerShell Cmdlets, Get-________ code line modification to get output in Text file

Post by DocAElstein »

Thanks for the suggestion Joe. I just gave it a quick try ( I tried both pwsh or PWSH ).
I tried in the short code below that is working in PS5, but it doesn’t seem to work.
( I tried using the computer where I installed the PS7 successfully, and where I got a few command things which I mentioned in the last post to do what they should when I typed them manually in the black PS7 window).

I have not looked thoroughly yet at the VBA PS7 issue. I may have missed some info on the internet.

This was the code I checked on , which is OK for PS5

Code: Select all

 Sub testit()   '   https://www.devhut.net/vba-run-powershell-command/          http://www.eileenslounge.com/viewtopic.php?p=292630#p292630
 'Call PS_Execute("Get-Service|Format-Table -AutoSize  name,displayname,starttype| Out-File -FilePath 'C:\Users\Elston\Desktop\test.txt' -Width 1000")
 Call PS_Execute("Get-Service|Format-Table -AutoSize  name,displayname,starttype| Out-File -FilePath '" & ThisWorkbook.Path & Application.PathSeparator & "test.txt' -Width 1000")
 'Call PS_Execute("Get-Service|Format-Table -AutoSize  name,displayname,starttype| Out-File -FilePath 'C:\Users\acer\Desktop\test.txt' -Width 1000")
 
 End Sub
 
Public Sub PS_Execute(ByVal sPSCmd As String)
    'Setup the powershell command properly
    'sPSCmd = "powershell -command " & sPSCmd  ' <--- OK for PS5
    sPSCmd = "pwsh -command " & sPSCmd
    'Execute and capture the returned value
    CreateObject("WScript.Shell").Exec (sPSCmd)
End Sub

( I also tried with startuptype in place of starttype. That made no difference, but I did not expect it to: you can usually put any words there, and if they don’t match up with a valid property heading then the code doesn’t error – it just gives you an empty column in the list )



_.___________________________________________________
Misc links not always directly relavant for later use
https://www.eileenslounge.com/viewtopic.php?t=33011


_.__________________________________________________________
Share ‘PowerShell.xls’ https://app.box.com/s/t65uuhic69g64yo4se2xuwopo9ezefa6
You do not have the required permissions to view the files attached to this post.
Last edited by DocAElstein on 17 Feb 2022, 18:23, edited 1 time in total.
I seriously don’t ever try to annoy. Maybe I am just the kid that missed being told about the King’s new magic suit, :(