Change the Show Hidden Items setting on a folder - windows

Where is the setting to "show / hide items" on a particular folder stored?
I looking to be able to change it programmatically, hopefully in Powershell.

So try this :
$file = "c:\temp\t.txt"
# Hidde $file
Set-ItemProperty -Path $file -Name attributes -Value ([io.fileattributes]::Hidden)
# Remove Hidden if set
Set-ItemProperty -Path $file -Name attributes -Value ((Get-ItemProperty $file).attributes -bxor ([io.fileattributes]::Hidden))

I try you're code but this part doesn't work:
Set-ItemProperty -Path $file -Name attributes -Value ((Get-ItemProperty $file).attributes -bxor ([io.fileattributes]::Hidden))
I just replace "hiden" by "normal" and it's work:
Set-ItemProperty -Path $file -Name attributes -Value ([io.fileattributes]::Normal)
Thank you.

Related

Inconsistency with my code, it seems to work on random occasions

I have made this code that is meant to download a picture and then set it as a wallpaper. My problem is that sometimes it works a couple of tries in a row and sometimes it doesn't work at all.(I have deleted the picture that is set to be downloaded after every single try.) I would be very happy if someone has the solution to this problem. Anyway,... here is the code:
Invoke-WebRequest https://wallpapercave.com/wp/wp69067421.jpg -o E:\skiniw.jpg
Function Set-WallPaper($Value)
{
Set-ItemProperty -path 'HKCU:\Control Panel\Desktop\' -name wallpaper -value $value
rundll32.exe user32.dll, UpdatePerUserSystemParameters
}
Set-WallPaper -value "E:\skiniw.jpg"
Try waiting for the file to be downloaded and test the file is actually there before setting it as the wallpaper:
Start-Job -Name DownloadWallpaper -ScriptBlock { Invoke-WebRequest "https://wallpapercave.com/wp/wp69067421.jpg" -o "E:\skiniw.jpg" }
Wait-Job -Name DownloadWallpaper
if (Test-Path "E:\skiniw.jpg"){
Function Set-WallPaper($Value){
Set-ItemProperty -path 'HKCU:\Control Panel\Desktop\' -name wallpaper -value $value
rundll32.exe user32.dll, UpdatePerUserSystemParameters
}
Set-WallPaper -value "E:\skiniw.jpg"
}

Change Windows 'List separator' using Power Shell

often I have to change some Windows regional settings and I'm using PowerShell to do that, but the 'List separator' is the only field I didn't find in culture object, any clue where to find it?
My code so far:
$culture = Get-Culture
$culture.DateTimeFormat.ShortDatePattern = 'dd/MM/yy' # 'Short date' in 'Region' window.
$culture.NumberFormat.NumberDecimalSeparator = '.' # 'Decimal symbol' in 'Customize Format' window.
$culture.NumberFormat.NumberGroupSeparator = ',' # 'Digit grouping symbol' in 'Customize Format' window.
Set-Culture $culture
EDIT:
After a lot of trials and errors, I couldn't make it work using the Culture, but I could make it work using registry (very weird):
Set-ItemProperty -Path 'HKCU:\Control Panel\International' -Name sDecimal -Value '.'
Set-ItemProperty -Path 'HKCU:\Control Panel\International' -Name sThousand -Value ','
Set-ItemProperty -Path 'HKCU:\Control Panel\International' -Name sList -Value ';'
I believe you're looking for:
(Get-Culture).Textinfo.ListSeparator

Retrieve a specific field from the result of Get-ItemProperty in Powershell

I've the below powershell script:
$registrypath = "HKLM:\SYSTEM\CurrentControlSet\Control\DeviceGuard"
$Name = "EnableVirtualizationBasedSecurity"
$ExpectedValue = "1"
$value = Get-ItemProperty -Path $registrypath -Name $Name
Write-Host($value)
Its output is:
#{EnableVirtualizationBasedSecurity=1; PSPath=Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\DeviceGuard; PSParentPath=Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control; PSChildName=DeviceGuard; PSDrive=HKLM;
PSProvider=Microsoft.PowerShell.Core\Registry}
I want to get the value of EnableVirtualizationBasedSecurity field in it to a variable in my powershell script.
Like $SpecificFieldValue = $value.get(EnableVirtualizationBasedSecurity);
How do i do it in powershell ?
Get-ItemProperty offers you an PSCustomObject as response.
This means you can directly get the value of the property like this:
$value.EnableVirtualizationBasedSecurity
or directly save the Value in the Get-ItemProperty-call like this:
(Get-ItemProperty -Path $registrypath -Name $Name).EnableVirtualizationBasedSecurity
or like this
Get-ItemProperty -Path $registrypath -Name $Name | Select-Object -Expandproperty EnableVirtualizationBasedSecurity
I think the problem is, that you expect the response to be an hashtable instead of an PSCustomObject.
You can get informations about the ObjectType of an Response by just adding () around a call and calling the getType() methode:
(Get-ItemProperty -Path $registrypath -Name $Name).GetType()

Powershell script to create text files in all the drives

I am new to PowerShell and looking for some help. Here I am trying to create a text file in all the drives but seems I am missing something here, any help is appreciated:
$drives = get-psdrive -p "FileSystem"
foreach ($drive in $drives)
{
New-Item -Path '$drive:\IO.txt' -ItemType File
}
Also, have another query regarding the same that how I can exclude particular drives e.g. "A:", "C:", "D:" drives?
Thanks
I think this is more what you are after.
$drives = get-psdrive -p "FileSystem"
$exclude = "C","D"
foreach ($drive in $drives) {
# Exclamation (!) is the same as -not meaning if not $true > Do the thing
If(!$exclude.Contains($drive.Name)) {
New-Item -Path "$($drive):\IO.txt" -ItemType File
}
}
Give New-Item -ItemType File -Name "Filename.ext" a shot,
see: https://mathieubuisson.github.io/powershell-linux-bash/
A more PowerShell like way is to filter the drives with a Where-Object and
do it in a single pipe
Get-PSDrive -PSProvider "FileSystem" |
Where-Object Name -notmatch 'A|C|D' |
New-Item -Path {"$($_.Name):\IO.txt"} -ItemType File -WhatIf
If the output looks OK remove the trailing -WhatIf

Unable to Set local group policy using powershell

I try to set local group policy using PowerShell. My goal is to enable
Disable changing home page settings
Below is the PowerShell script:
# Set the values as needed
$dchps_control_panel = "HKCU:\Software\Policies\Microsoft\Internet Explorer\Control Panel"
$dchps_main = "HKCU:\Software\Policies\Microsoft\Internet Explorer\Main"
$homepage = "HomePage"
$enabled = 1
$startpage = "Start Page"
$startpage_value = "www.google.com"
# Test if the Registry Key exists already
if(-not (Test-Path $dchps_control_panel) -or -not (Test-Path $dchps_main) )
{
# Create Control Panel Key to enable and Main Key
New-Item -Path $dchps_control_panel -Force
New-Item -Path $dchps_main -Force
}
# Enable Disable changing home page settings
Set-ItemProperty -Path $dchps_control_panel -Name $homepage -Value $enabled -Type DWord
#
# Set Start Page
Set-ItemProperty -Path $dchps_main -Name $startpage -Value $startpage_value -Type String
The registry keys both get created. However, when I check the "gpedit.msc" the setting still remains to disable and nothing was configured.
Thanks

Resources