Link to home
Start Free TrialLog in
Avatar of ranski
ranski

asked on

Drop File via Powershell

Hi all,

oBdA was very helpful in proving the below code. I need to however expand it slightly and allow it to only run once.

I thought the best way to achieve this would be to create a file once it's ran. Then any future logins the script will check for that file and if present will not change the power scheme setting again. I would like the file to be called PowerSettingMod.txt and inside a line of text stating "The power setting was successfully applied on DATE"

If ((& powercfg.exe /GetActiveScheme) -match '(?<GUID>[a-f0-9]{8}-(?:[a-f0-9]{4}-){3}[a-f0-9]{12})') {
	$currScheme = $Matches['GUID']
	"Current scheme is '$($currScheme)'" | Write-Host
	& powercfg.exe /SetDCValueIndex $currScheme '4f971e89-eebd-4455-a8de-9e59040e7347' '5ca83367-6e45-459f-a27b-476b1d01c936' 0
} Else {
	"Could not query the active scheme!" | Write-Error
}

Open in new window

Avatar of oBdA
oBdA

$FlagFile = "C:\Temp\PowerSettingMod.txt"
$FlagText = "The power setting was successfully applied on $(Get-Date)"
If (Test-Path -Path $FlagFile) {
	"Change has already been applied!" | Write-Warning
} Else {
	If ((& powercfg.exe /GetActiveScheme) -match '(?<GUID>[a-f0-9]{8}-(?:[a-f0-9]{4}-){3}[a-f0-9]{12})') {
		$currScheme = $Matches['GUID']
		"Current scheme is '$($currScheme)'" | Write-Host
		& powercfg.exe /SetDCValueIndex $currScheme '4f971e89-eebd-4455-a8de-9e59040e7347' '5ca83367-6e45-459f-a27b-476b1d01c936' 0
		$FlagText | Set-Content -Path $FlagFile
	} Else {
		"Could not query the active scheme!" | Write-Error
	}
}

Open in new window

Avatar of ranski

ASKER

Top! thanks.

Any chance we can get the date in UK format?
The power setting was successfully applied on 11/03/2016 21:26:50
The power setting was successfully applied on 03/11/2016 21:26:50

I also noticed that it fails unless I create the Temp folder first
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ranski

ASKER

Thanks,

I get the below error

At C:\Untitled1.ps1:13 char:42
+         If (-not (Test-Path -Path $FlagFolder) {
+                                                ~
Unexpected token '{' in expression or statement.
At C:\Untitled1.ps1:13 char:42
+         If (-not (Test-Path -Path $FlagFolder) {
+                                                ~
Missing closing ')' after expression in 'If' statement.
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : UnexpectedToken
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ranski

ASKER

perfect thanks.

The time still reports as US but not a bid deal really

The power setting was successfully applied on 11/03/2016 21:58:10
Just swap the "dd" and "MM" (case sensitive!) in line 2 if you want the day first.