Creating a Power Mode Script in Windows 10 - windows

Background:
I'm interning at a firm this fall in an IT support position, one of the ongoing projects I'm fielding is the development of useful powershell scripts for our machines. Recently we've been having trouble with our new machines constantly trying to reset their policy scheme. Adjusting the power mode slider to performance works to fix this issue. We're in talks with our vendor to find a more permanent solution. That being said, I would like to develop a script that can automatically adjust this slider. Here's a picture of the slider for clarity.
I have a few scripts that adjust power settings that I know for a fact work. Here's an example:
$activeScheme = cmd /c "powercfg /getactivescheme"
#Ensure active power scheme guid matches a set format
$regex = '(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}'
$asGuid = [regex]::Match($activescheme,$regex).Value
#obtain necessary guid's to execute
$pwrGuid = '4f971e89-eebd-4455-a8de-9e59040e7347'
$lidClosedGuid = '5ca83367-6e45-459f-a27b-476b1d01c936'
#Set value, 0 for does nothing, 1 for sleep
cmd /c "powercfg /setdcvalueindex $asGuid $pwrGuid $lidClosedGuid 0"
#Apply setting
cmd /c "powercfg /s $asGuid"
Is there a similar route I can take to adjust this slider value? I tried pulling the guid of what I believe is the slider and substituting it into that example code. Any ideas on how to approach this? Thanks.

Related

How to automatically set the minimum and maximum paging file size?

So one of the applications that we use is consistently crashing whenever it is processing a larger amount of data than it usually does, so to mitigate this I found a fix for it where I can manually increase the paging file size of the windows 10 OS. I've attached screenshots of what I accessed and what values I've changed. Is there any way to have the same thing done with a power shell script? There are a lot of people in my department who most likely won't want to go through the process of navigating control panel and manually changing these values.
Is there a power shell script that can manually edit the virtual memory pop-up and change it from the checkbox that says "Automatically manage paging file size for all drives" to setting it to the custom size radio button, and inputting value 12000 for initial size and value 32000 for maximum size? These options can be found by typing "Adjust the performance and appearance of windows" on the search bar, navigating to the advanced tab, and finally clicking change under virtual memory. I know this is a bit of an unorthodox request but any help on this would be really appreciated. Thanks!
According to this answer on a similar question:
You can modify registry in order to change pagefile settings.
They are stored in the following registry's key:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management
PagingFiles value contains values in the 'PageFileLocation MinSize MaxSize' format (i.e. 'C:\pagefile.sys 1024 2048')...
and it's possible to create or modify registry entries with PowerShell (see "Example 2: Create a registry entry and value").
According to a comment on the same answer, there's also a method of modifying the values using WMI. A Google search yielded this Microsoft documentation on the Win32_PageFileSetting class, which you can interface with using PowerShell.
PowerShell
# Run as administrator
# Step1. Disalbe AutomaticManagedPagefile
$ComputerSystem = Get-WmiObject -ClassName Win32_ComputerSystem
$ComputerSystem.AutomaticManagedPagefile = $false
$ComputerSystem.Put()
# Step2. Set Pagefile Size
$PageFileSetting = Get-WmiObject -ClassName Win32_PageFileSetting
$PageFileSetting.InitialSize = 12000
$PageFileSetting.MaximumSize = 32000
$PageFileSetting.Put()
CMD.exe / Bat
:: Run as administrator
wmic COMPUTERSYSTEM set AutomaticManagedPagefile=false
wmic PAGEFILESET set InitialSize=12000,MaximumSize=32000

How to change admin name and logout threshold with powershell win2k12 R2

I am trying to automate some regular tasks, and I need some help. Does powershell compile like C++ or is a simple batch file like the old .bat
Is there an online lint/editor for powershell place like jsfiddle?
Main question: I need help with automating some of these into a powershell script (both interactive and non-interactive modes) and looking at if they succeed
Change user/admin name Get-WMIObject Win32_UserAccount -Filter "Name -like 'admin*'" | Foreach-Object {$_.Rename("Dingbats)")}
Turn on lockout threshold to 3 attempts and set it to 45 mins
PS C:\> Set-ADDefaultDomainPasswordPolicy -Identity SS64.com -LockoutDuration 00:40:00 -LockoutObservationWindow 00:20:00 -ComplexityEnabled $true -ReversibleEncryptionEnabled $false -MaxPasswordAge 10.00:00:00
another example
# set lockout threshold value
# how do I **change $mydomain to my server name** or local server automatically??
PS C:\> $mydomain.MaxBadPasswordsAllowed = 0
# set lockout duration value (in seconds)
PS C:\> $mydomain.AutoUnlockInterval = 1000
Turn on/enabled windows update service to start/auto on window startup
..
Edit 1: I posted some code before, now I have added other snippets as requested, I am still working on figuring out the auto start of windows updates. The challenge seems to be that - there are many options to do the same thing in Powershell. There seems to be an incredible amount of power, and the danger of messing up your system. So, I am looking for help in consolidating so I can add and maint the scripts on my own.
PS is a scripting language - which means it is interpreted, like Python, Ruby, Perl, and, yes, CMD.EXE .BAT files. However there is a huge difference between the capabilities of the two.
Regarding lint, there is the set-strictmode command to diagnose some errors that wouldn't otherwise be called out. However, a scripting language is significantly different from a language like C (to which lint is applicable). Some of the dangerous things you could do in C, that lint would diagnose, just can't be done in a scripting language.
As far as your 3 items, SO is meant to help folks get help with coding. But you don't have much code posted, and it isn't clear if the code you do have works or if you're having trouble with it.
To get started I suggest googling for the three tasks (or last two if the line of code you have works), but add the word Powershell to your search.
You may also want to look at some tutorials on basic PS script. You could learn basic scripting in an hour or less of searching and reading.

Windows Server: audit space usage (latest files/folders added and size)

i work in a design agency (designer, little knowledge on programming). we have a Windows Server machine (Windows Storage Server 2008) that holds all our jobs. it's going to go under maintenence soon, because it got really slow after a thunderstorm.
however, i'd like to know if there's a native way or something that can be done to find out the latest files/folders created and the space they use. because in the last month, there's been a huge increase in the space usage, coming to something like 20GB in one single day (it shouldn't be more than 4GB/day).
i'm looking for a way to find which jobs are with this unnecessary extra space and help people work the right way.
thank you!
There's no simple way to do this that i know of - however if you're ok with using the command prompt (in the server of course) then this answer from #learnScrapy at SuperUser (with a few tweeks) should do the trick.
Something like:
forfiles /P D:\ /M *.* /S /D +"01/17/2012" /C "cmd /c if #fsize gtr 209715200 echo #path #fsize #fdate #ftime"
You'd need to change D:\ to whatever path you store all your design work at, the date ("01/17/2012") would need to be changed to from whatever date you know the issue wasn't present yet (and maybe change the order depending on the server's regional settings (MM/DD/YYYY vs DD/MM/YYYY vs ....)), and finally change the value 209715200 to whatever minimum size you'd like to search files for - that value is 200MB.
If your projects span multiple files though, if all are smaller then the size you choose but there's just a awful lot of them making up for several GB then you can't know and this won't work. This will only find files with a date later than the one you specified with size larger then the one you specified in the path you specified.

MS-DOS Pipe the content of a file into a variable

I am trying to send the content of a text file (which is just one word) into a variable in MS-DOS.
I tried doing it with pipes like so ,without any success
TYPE username.txt | %savedName%
Can anyone enlighten me?
for /f "delims=" %%i in (username.txt) do set "savedname=%%i"
echo savedname=%savedname%
should work for you (as a batch file). If you are executing directly from the prompt, then reduce each %% to %.
If you are nunning this on a Windows machine using WIN NT4, Win2000, WINXP, WIN7, Vista or Win8 then this should work (as also the set/p approach should have worked)
If you are using Win95, Win98, WinME or real MSDOS, then a different approach would be required.
"MSDOS" is often used to mean "Command Prompt" - a generic term ridiculously misapplied to mean "A windows application which emulates the functionality of the MSDOS command-interpreter (with enhanced functionality)". Unfortunately, since "AWAWETFOTMCI(WEF)" is such a mouthful, many people abbreviate it to "MSDOS" or "DOS". This raises the ire of that sad section of the computing community that is more interested in asserting that MSDOS no longer exists than in communicating effectively.
this one is working fine and simpler
set /p SCHEMAS=<schemas_file.txt

Minimizing and Maximizing Windows With VBscript or Batch files

I'm trying to find a way to maximize & minimize a certain application using vbscript or a batch file. I've seen multiple attempts at doing this but none of them seem to work. Does anybody have any suggestions, without having to download any third party software.
I think that /MIN and /MAX switches of START command should work, that is:
START "Window Title" /MAX YourApplication Parameters ...
Type START /? for further details.
If you are trying to maximize the batch window, you can use the code
Mode Con Cols=500 Lines=500
what this does is determine how many text blocks up and down there are, for example, if you changed cols to 20, you could fit twenty letters in that window. And Lines are the same way, but going up and down.
:edit
sorry, I read your question wrong. either way, my answer was wrong. I think the only way is to maximize it from startup.

Resources