Powershell Version: 4.0
Operating System: Windows 7
"Error Report" | Set-Content $errorText
$getError = Get-Item $errorText
$getError.Attributes ="Hidden"
I've created an error log file which will remain hidden unless their is an error. How would I remove the hidden attribute so that I could see my file? In other words what is the equivalent of right clicking the file, selecting properties, and unchecking hidden attribute in powershell?
Attributes property is of type [System.IO.FileAttributes]. Looking at TechNet you can see the valid options for setting attributes on file. As per the comments one option you have is no remove all attributes from the file/folder.
Normal - The file is a standard file that has no special attributes. This attribute is valid only if it is used alone.
Be aware that this would potentially remove other attributes like read-only or system. In your case that might not be a concern but we need to be aware of the possibility.
The related second issue about using Get-Item on hidden files is solved by using the switch -Force and explained on TechNet as well.
Allows the cmdlet to get items that cannot otherwise be accessed, such as hidden items.
Knowing that we could do something like this:
$getError = Get-Item $errorText -Force
$getError.Attributes ="Normal"
Related
Using Powershell, I want to create script to read various executable's version numbers and name. The info is listed on the details tab of the file's property sheet:
The basic file system object's properties do not seem to list them (using get-member). Also, have reviewed the following StackOverflow suggestions, that were not immediately helpful:
stackoverflow ref1
Stackoverflow ref2
Is it possible to do this with powershell?
In your case, the .VersionInfo property of the target executable's System.IO.FileInfo instance, as reported by Get-ChildItem or Get-Item, contains the information of interest:
$exeFile = 'C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe'
(Get-ChildItem $exeFile).VersionInfo.ProductVersion
To see all available properties:
$exeFile = 'C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe'
(Get-ChildItem $exeFile).VersionInfo | Format-List *
Note that a different approach is required if different kinds of properties are to be extracted from files, such as photo-related metadata from image files - see this post.
Is it possible to set default values for existing commands in PowerShell?
What I specifically want to do is to tell the Get-ChildItem command to show both normal and hidden files (Get-ChildItem -Force).
I know I can write a function where I can add this option and use whatever else is specified on the command line. But then I lose the auto-completion functionality for all parameters and options.
You should be able to do so using $PSDefaultParameterValues. Has worked from 3.0+.
Microsoft's documentation:
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_parameters_default_values?view=powershell-7.1
Here is a set of examples directly from that documentation:
$PSDefaultParameterValues=#{"CmdletName:ParameterName"="DefaultValue"}
$PSDefaultParameterValues=#{ "CmdletName:ParameterName"={{ScriptBlock}} }
$PSDefaultParameterValues["Disabled"]=$True | $False
I mistakenly turned C:\Users\User into a hidden folder. Now, a lot of my programs aren't running. I can't even open file explorer. I managed to open powershell in admin mode, but am able to see the hidden folders using ls - Force. However, I don't know how to unhide them. How to I remove the the hidden attribute so that the mode of the folders change from d--h-- to d-----?
As Lee_Dailey points out, you can edit the Attributes property value on the corresponding DirectoryInfo object.
Since Attributes is a Flags enum (or a bit field if you will), the easiest way to remove a flag is to use bitwise operators:
$folder = Get-Item C:\Users\User -Force
$folder.Attributes = $folder.Attributes -band -bnot [System.IO.FileAttributes]::Hidden
This will set the value of Attributes to whatever it already is, but excluding the Hidden flag regardless of whether it was set or not
Mathias R. Jessen's helpful answer shows how to use PowerShell-native commands to solve your problem, but on occasion it is easier to call an external program to do a simple job, which is the standard attrib.exe utility in this case:
# Remove (-) the hidden (h) attribute from the specified dir.
attrib -h C:\Users\User
Note: Hypothetically speaking, if the target file or directory also has the system (s) attribute set - which you shouldn't modify yourself, and it is similarly ill-advised to change the hidden status of true system files - you would need attrib -s -h ... in order to turn off the hidden attribute (you could then restore the system attribute with attrib +s ...).
To access or make changes to registry in a windows machine by powershell, I see two ways
cd HKLM:\ (or set-location -path
HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion)
Get-childitem
or
Get-Item -path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
Get-ItemProperty , New-ItemProperty , Set-ItemProperty
When to use one over the other?
They are both necessary, because the property values aren't accessible on the Item objects (and you can't see the nested "child" container items with the ItemProperty commands).
My 2c: the registry provider was written by someone as a demo, and then shipped and now they're afraid to change it ... it's the only way to explain the whole "ItemProperty" thing.
Basically, when they mapped the registry to the PowerShell Provider semantics, instead of having "Container" and "Leaf" items, there are only containers, and each container has properties (and/or child containers).
So if you do Get-ChildItem HKCU:\SOFTWARE\Microsoft you get a response back listing the "Name" of each of the child containers (which the Registry Editory (Regedit.exe) would show as folders), and listing the values in them under the heading "Property" -- but that listing is purely in the display, and the Property field on the object you got back is actually just a string array listing the names of the properties so you know what you can do next:
If you want to actually read the values in a way that makes them accessible to your script, you need to use Get-ItemProperty
If you want to change the value, you need to use Set-ItemProperty
To create new ones, you need to use New-ItemProperty
In your example, for instance, you might see SecurityHealth:
But it's like the screenshot above, as a human, you can see the value, but your script can't read the value...
You have to use Get-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run to get an object back that actually has the SecurityHealth property with the C:\WINDOWS\System32\SecurityHealthSystray.exe value...
My script is supposed to rename c:\myfolder\myfile.txt to myfile.bak
Rename-Item "c:\myfolder\myfile.txt" -NewName "myfile.bak"
However, it leaves myfile.txt in place and creates a new file called myfile.bak instead
Does anyone know why?
If its a hidden or read-only file, you have to add the -force parameter:
Forces the cmdlet to rename items that cannot otherwise be changed,
such as hidden or read-only files or read-only aliases or variables.
The cmdlet cannot change constant aliases or variables. Implementation
varies from provider to provider. For more information, see
about_Providers.