Write a windows script - windows

I would like write a script to automate a certain task on windows.
I use a monitor to extend the screen of my laptop but every time I remove the plug and reconnect the monitor I have to go on to desktop properties and display settings to extend the monitor and set the resolution.
Well I assume on LINUX you can do this by using shell scripting but I am not sure how to do this in windows.
From my research on the internet I have found that BASH scripts could be used to do this task automatically.
Please guys I don't need a software to do this I would like to write the script myself so that I can learn something as well.
I have read up on bash scripting but I cant find methods to access windows components and just to let you know I am an intermediate JAVA programmer. I guess I could use JAVA as well.
I hope my questions are clear.
Thank You

Bash scripting is for *nix systems, the equivalent on windows is Batch Scripting.
Some quick googling indicates that this can be done through registry edits, but they would not take effect until the next reboot.
Other than that, it doesnt look like doing that through batch scripting is possible.
If you want to look at another programming language to do this, check out this answer

Related

Finding PowerShell Cmdlets equivalent to GUI actions

I would like to know where I could find good resources/documentation on configuring a new Windows10 installation using Powershell scripts. I know bash but I'm completely new to Powershell.
When I search google, all I can find about automatically configuring Windows relates to Windows Deployment Services. But I don't have and don't want a Windows Server and simply running a few scripts after each installation is sufficient for me.
I found a few scripts that solve some of the things I want to do:
https://github.com/FlatlanderWoman/winCleaner
https://github.com/hahndorf/Set-Privacy
But for everything else, when I look into the TechNet Library I find it very hard to find anything useful. And when I do find something, it looks outdated:
https://technet.microsoft.com/en-us/library/hh852115.aspx
The problem is: I known the GUI-way of configuring everything I want, but I don't know how to find the corresponding commandlets to do the same with Powershell.
Is there some kind of event listener I could use to find the Cmdlets? Or does anyone have some resources/documentation to recommend? Is the TechNet Library really the established way to find these commands?
Thank you.
Unfortunately PowerShell was only really implemented in Windows 7 (yes I know it was available for XP but not preinstalled) and even then it was kind of like an addon rather than part of the core OS. Windows 8 and 10 have further improved functionality but still for the most part do not use it for their own settings and functions as most home users would have no use for it.
However there is nearly always a way to do whatever you need to, I have a script that configures servers from scratch, renaming the server, installing requisite software and features, copying files, configuring VSS, right down to putting the Computer icon on the desktop. You just have to make a list of everything you want to do, then Google each one.
For example: https://www.google.co.uk/search?q=powershell+put+computer+on+desktop - at time of writing the first result is a TechNet script pointing at a registry key. Tidy as necessary, whack into your build script and move on to the next item.
As of yet there's nothing I've found I've been unable to do with PowerShell, but the vast majority of it has not been directly with cmdlets. There's a lot of registry tweaking and command line stuff like msiexec or schtasks, some COM objects and an awkward Type I had to create and use to set the DNS suffix.
Overall I think it's still easier to do all this in PowerShell than any other scripting language and it's more flexible than premade tools, not because it has so much functionality built-in but because it can access .NET and COM which gives you broad access to all the half-baked stuff MS have wedged in over the years.

How can I gather system information via scripting?

My original question was to quickly and easily gather hardware information from a computer. I was also hoping to do this over a network, rather than sit down at each PC and run a script. Scriptomatic 2.0 was the solution to my problem.
One thing I would like to note about it: It seems to work well only under Windows XP. When I ran it on Windows 7, it did not function properly.
During my long night of trying to solve the problem myself, I came across this wonderful tool. It had a ton of sample scripts, all of which applied to my situation.
http://www.vbsedit.com/scripts/misc/wmi/scr_1343.asp
This program is free by evaluation. The evaluation will never run out however, as stated by the program. Each time you compile, it takes a second longer. However, if you open a new window the counter resets. This program was extremely helpful for someone like me who knows basic programming, but nothing about VB scripting.
Microsoft has provided a free tool called Scriptomatic 2.0 which automatically generates scripts in vbscript that allows you to get hardware and software information. You can download the tool from http://www.microsoft.com/en-in/download/details.aspx?id=12028

Best way to automate tasks in windows

Mac has applescript 'built in' and I've found its pretty nice to work with for automating stuff.
What's the best windows equivalent?
Maybe PowerShell -- but it's really much more powerful than applescript. VBA only exists within specific applications that supply it, such as Excel, not stand-alone.
VBScript and JScript are standard Windows Script Host languages. See Microsoft Windows Script Technologies.
I discovered python when I moved to a PC and was looking for something to do all the kind of stuff I'd usually do with applescript. As someone who learned most of my programming skills using applescript it came pretty easily to me. Best thing is that you can use it on any platform.
BTW applescript has been around a lot longer than OSX.
Powershell is the program to learn if you want to automate stuff in windows. Microsoft is deprecating vbscript, which is part of Windows Script Host, and pushing Powershell as the modern replacement for vbscript. For example, Microsoft is creating more and more APIs for Powershell. On the other hand, vbscript bears a great similarity to vba, which is the default program for automating Word, Excel and Outlook.
Just for completeness, there is also the rather dated AutoHotkey. Originally designed to provide system-wide hotkeys to automate things, it can also be used as a general automation tool.
However, I cannot recommend it. The syntax is horrible and obscure, and that's the reason they are currently doing a complete haulover for version 2.

What tools and languages are available for windows shell scripting?

I want to know what are the options to do some scripting jobs in windows platform.
I need functionality like file manipulations, registry editing etc. Can files be edited using scripting tools?
What other functionality does windows scripting tools offer?
Can everything that can be done using the Windows GUI be done using a scripting language?
I think Windows PowerShell from Microsoft is the current favourite for this sort of thing.
It might be worth looking at the prerelease of version 2.0. A lot of stuff has changed:
http://blogs.msdn.com/powershell/archive/2007/11/06/what-s-new-in-ctp-of-powershell-2-0.aspx
How about installing a windows version of Python, Perl or your favorite language? These should offer all the functionality you need.
Batch files are the most portable, but doing complicated things can get hard (very hard).
Powershell is incredibly - um - powerful, but the installed domain at the moment is only slightly more than those people who like using powershell and servers they administer. If you control the machines you're scripting on and can mandate that powershell is installed, powershell is the way to go. Otherwise, batch files are the best way.
Powershell lets you do anything which can be done, but some things will be harder than others :)
(Seriously, if you want to control a windows GUI app from a script, you're going to have a lot of pain unless the app supports scripting itself, or you want to start posting messages to dialog controls and screen scraping the dialog to check for success.)
I would recommend "Take Command" (JPSoft), which is more like "cmd.exe" than the PowerShell. We use here at ESSS for years.
Windows Scripting Languages
Also take a look at PowerShell
CScript ? I remember seeing something like that.
Powershell is nice, but it's an extra thing you have to install. Not standard in almost any windows installation. So, if it's just for your own use, then powershell should be fine. If you need the scripts to run on the computers of the general population, for instance, as part of a piece of software you are producing, it may not be the best option.
If you are ok with an extra thing you have to install, you might want to check out cygwin. This allows you to have a full Linux bash command line and all the associated tools at your disposal.
If you need something included in a default windows install. There's the windows command line (cmd.exe). Which has some functionality, but is very deficient compared to what's available in Linux. The other, probably worse problem, is that there isn't much out there in the way of documentation.
You might also be interested in VB Script (flame away). VB Script should work on almost any recent standard windows install, and is a lot more functional than the command line.
I have cygwin installed, so I can run bash shell scripts for my automatization needs. Besides, when I need stuff running moreless natively on Windows, I use a combination of batch + jscript (runs on cmdline if you have Visual Studio.Net installed, just call "cscript XXX.js").
Scripting is a blast.
Personally I like to write some evil little batch files. You can find a command line program to do just about anything. I prefer Batch files mostly because they are portable from one machine to another with at most a zip with a few unix tools (SSED, GREP, GAWK). there is a commandline REG.Exe that can even do Registry changes and reads. You can parse output from commands with a "FOR /f" loop.
PowerShell does have more... err.. Power (2nd post I wrote that in, but I can't help it.)
If you want to look at windows automation check out AutoHotKey.
What are you trying to automate? That may help us narrow down what would be helpfull.
Josh
EDIT: for the record I typed that at the same time as #jameso If someone at work hadn't asked me a question, I may have posted before him. I did get a bit of a shiver at the similarity of the post though....
Powershell can do what you need.
file manipulations
This SO post answers how you can replace a string in your text file. Pasting it here for easy reference:
(Get-Content c:\temp\test.txt).replace('[MYID]', 'MyValue') | Set-Content c:\temp\test.txt
There are other things that you can do, such as copying files and folders. You can find out more on the Windows Powershell Documentation
registry editing
This can be easily done using Powershell. Here's a sample code from Microsoft Dev Blogs:
Set-ItemProperty -Path HKCU:\Software\hsg -Name newproperty -Value anewvalue
Yesterday I could have repaired this for you ;)
What all are the tools/languages for
windows shell scripting?
Would read better as
What tools and languages are available
for windows shell scripting?

Easy installation method for windows/ Batch Reference needed?

I have a bunch of files that I need to be able to transport and install quickly. My current method for doing so is moving a flash drive with a readme file of where stuff goes whenever I need to move stuff, which is rather inelegant and cumbersome.
My idea for a solution would be to write up a quick script to move files around that I could just click on. I've done some bash scripting before but batch scripting is a little odd to me. Does anyone have a good online reference guide I could use?
An alternative soulution I could accept would be a program that makes an installer for you, though I'm a bit against that as I would lose a lot of control. However, I'd be alright with it if it was extremely simple,
Sounds like robocopy tool is exactly what you need.
Very powerful replication command-line tool.
MS TechNet reference,
Wikipedia article about robocopy,
Full command switch guide,
Batch scripting guide.
I like to use VBscript for this kind of thing. The VBS engine is on every recent windows machine and the language is a little more like real programming than a batch script.
Also, if your installer grows to require WMI functions too, this becomes a piece of cake.

Resources