使用Powershell隐藏父目录和子目录

使用Powershell隐藏父目录和子目录,powershell,Powershell,这是我的问题。我正试图找出如何隐藏父控制器和我用powershell创建的所有子目录。这是我正在使用的代码。。。 $f=ni-ItemType目录-Path'C:[name][name][name]\'-Force $f.attributes='Hidden' 我的问题是,它只会隐藏最后一个文件夹,而不会隐藏整个路径。请帮帮我,我没有头发和牙齿了 $f=ni-ItemType目录-Path'C:[name][name][name]\'-Force $f.attributes='Hidden' $

这是我的问题。我正试图找出如何隐藏父控制器和我用powershell创建的所有子目录。这是我正在使用的代码。。。 $f=ni-ItemType目录-Path'C:[name][name][name]\'-Force $f.attributes='Hidden' 我的问题是,它只会隐藏最后一个文件夹,而不会隐藏整个路径。请帮帮我,我没有头发和牙齿了

$f=ni-ItemType目录-Path'C:[name][name][name]\'-Force $f.attributes='Hidden'

$f=ni-ItemType目录-Path'C:[name][name][name]\'-Force $f.attributes='Hidden'


预期结果是隐藏父目录和子目录。实际上只隐藏了最后一个子目录。

改为在C:\Name上设置属性。

改为在C:\Name上设置属性。

首先,为什么要设置wo行? 他们正在做完全相同的事情

这不是特定于PowerShell的问题。这是一个Windows文件系统约束。因此,如果您在DOS(cmd.exe)或任何其他语言上执行此操作并尝试此操作,同样的情况也会发生

属性应用于传递给它的单个对象。目录树不是一个单一的东西,它是一个集合

所以,如果你想做什么,你就必须迭代树

最后,如果隐藏父对象,我不确定为什么需要隐藏子对象,因为它们在默认情况下是隐藏的(即使未设置属性),除非您知道父对象的名称和导航到子对象的路径。然而,无论哪种方式发现隐藏的东西都非常简单。所以,我不确定你想通过这个来实现什么

不管怎样,这就是我的意思

New-Item -ItemType Directory -Path 'E:\Parent\Child\GrandChild' -Force

# Results

<#
    Directory: E:\Parent\Child


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         7/7/2019   7:16 PM                GrandChild 

Notice that doing what you are doing, only returns the last thing, by Windows 
file system design. This is why you are only getting the setting on the one 
item.
#>



# Not all of them that were created - To get all things in a tree, you have to recurse.
Get-ChildItem -Path 'E:\Parent' -Recurse

# Results

<#

    Directory: E:\Parent


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         7/7/2019   7:16 PM                Child


    Directory: E:\Parent\Child


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         7/7/2019   7:16 PM                GrandChild
#>


<#
Since there is no -recurse in New-Item, as per the help files info ...

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/new-item?view=powershell-6

... after doing the first line to create the tree, iterate to set the attribute 
on the children and grandchildren, etc., first,  then act on the parent.

Again, breaking down the object.
#>


($NewTree = New-Item -ItemType Directory -Path 'E:\Parent\Child\GrandChild' -Force)

# Results

<#
    Directory: E:\Parent\Child


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         7/7/2019   7:16 PM                GrandChild 
#>

$NewTree.Name

# Results
# GrandChild


$NewTree.FullName

# Results
# E:\Parent\Child\GrandChild

$NewTree.Parent

# Results

<#
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         7/7/2019   8:10 PM                Child  
#>

$NewTree.PSChildName

# Results
# GrandChild


Get-ChildItem -Path 'E:\Parent' -Recurse

# Results 

<#
 Get-ChildItem -Path 'E:\Parent' -Recurse


    Directory: E:\Parent


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         7/7/2019   8:30 PM                Child


    Directory: E:\Parent\Child


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         7/7/2019   8:30 PM                GrandChild
#>

# So, iterate the children and set the attribute

($ChildFolders = (Get-ChildItem -Path 'E:\Parent' -Recurse)) | 
ForEach{$PSItem.Attributes = 'Hidden'}

# Then set the final parent.
($ParentFolder = Get-ChildItem -Directory -Path 'E:\' | 
Where Name -eq 'Parent').Attributes = 'Hidden'

Get-ChildItem -Path 'E:\Parent' -Recurse

# Resutls

<#

#>


Get-ChildItem -Path 'E:\Parent' -Force -Recurse

# Results

<#
 Get-ChildItem -Path 'E:\Parent' -Force -Recurse


    Directory: E:\Parent


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d--h--         7/7/2019   8:39 PM                Child


    Directory: E:\Parent\Child


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d--h--         7/7/2019   8:39 PM                GrandChild
#>
newitem-ItemType目录-Path'E:\Parent\Child\grant'-Force
#结果
#并不是所有的树都是被创建的——为了得到树中的所有东西,你必须递归。
获取子项-路径“E:\Parent”-递归
#结果
($NewTree=New Item-ItemType目录路径'E:\Parent\Child\grant'-Force)
#结果
$NewTree.Name
#结果
#孙子
$NewTree.FullName
#结果
#E:\Parent\Child\孙子
$NewTree.Parent
#结果
$NewTree.PSChildName
#结果
#孙子
获取子项-路径“E:\Parent”-递归
#结果
#因此,迭代子项并设置属性
($ChildFolders=(获取ChildItem-Path'E:\Parent'-Recurse))|
ForEach{$PSItem.Attributes='Hidden'}
#然后设置最后一个父级。
($ParentFolder=Get-ChildItem-Directory-Path'E:\'
其中Name-eq'Parent')。Attributes='Hidden'
获取子项-路径“E:\Parent”-递归
#结果
获取子项-路径“E:\Parent”-强制-递归
#结果
首先,为什么要使用wo生产线? 他们正在做完全相同的事情

这不是特定于PowerShell的问题。这是一个Windows文件系统约束。因此,如果您在DOS(cmd.exe)或任何其他语言上执行此操作并尝试此操作,同样的情况也会发生

属性应用于传递给它的单个对象。目录树不是一个单一的东西,它是一个集合

所以,如果你想做什么,你就必须迭代树

最后,如果隐藏父对象,我不确定为什么需要隐藏子对象,因为它们在默认情况下是隐藏的(即使未设置属性),除非您知道父对象的名称和导航到子对象的路径。然而,无论哪种方式发现隐藏的东西都非常简单。所以,我不确定你想通过这个来实现什么

不管怎样,这就是我的意思

New-Item -ItemType Directory -Path 'E:\Parent\Child\GrandChild' -Force

# Results

<#
    Directory: E:\Parent\Child


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         7/7/2019   7:16 PM                GrandChild 

Notice that doing what you are doing, only returns the last thing, by Windows 
file system design. This is why you are only getting the setting on the one 
item.
#>



# Not all of them that were created - To get all things in a tree, you have to recurse.
Get-ChildItem -Path 'E:\Parent' -Recurse

# Results

<#

    Directory: E:\Parent


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         7/7/2019   7:16 PM                Child


    Directory: E:\Parent\Child


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         7/7/2019   7:16 PM                GrandChild
#>


<#
Since there is no -recurse in New-Item, as per the help files info ...

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/new-item?view=powershell-6

... after doing the first line to create the tree, iterate to set the attribute 
on the children and grandchildren, etc., first,  then act on the parent.

Again, breaking down the object.
#>


($NewTree = New-Item -ItemType Directory -Path 'E:\Parent\Child\GrandChild' -Force)

# Results

<#
    Directory: E:\Parent\Child


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         7/7/2019   7:16 PM                GrandChild 
#>

$NewTree.Name

# Results
# GrandChild


$NewTree.FullName

# Results
# E:\Parent\Child\GrandChild

$NewTree.Parent

# Results

<#
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         7/7/2019   8:10 PM                Child  
#>

$NewTree.PSChildName

# Results
# GrandChild


Get-ChildItem -Path 'E:\Parent' -Recurse

# Results 

<#
 Get-ChildItem -Path 'E:\Parent' -Recurse


    Directory: E:\Parent


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         7/7/2019   8:30 PM                Child


    Directory: E:\Parent\Child


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         7/7/2019   8:30 PM                GrandChild
#>

# So, iterate the children and set the attribute

($ChildFolders = (Get-ChildItem -Path 'E:\Parent' -Recurse)) | 
ForEach{$PSItem.Attributes = 'Hidden'}

# Then set the final parent.
($ParentFolder = Get-ChildItem -Directory -Path 'E:\' | 
Where Name -eq 'Parent').Attributes = 'Hidden'

Get-ChildItem -Path 'E:\Parent' -Recurse

# Resutls

<#

#>


Get-ChildItem -Path 'E:\Parent' -Force -Recurse

# Results

<#
 Get-ChildItem -Path 'E:\Parent' -Force -Recurse


    Directory: E:\Parent


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d--h--         7/7/2019   8:39 PM                Child


    Directory: E:\Parent\Child


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d--h--         7/7/2019   8:39 PM                GrandChild
#>
newitem-ItemType目录-Path'E:\Parent\Child\grant'-Force
#结果
#并不是所有的树都是被创建的——为了得到树中的所有东西,你必须递归。
获取子项-路径“E:\Parent”-递归
#结果
($NewTree=New Item-ItemType目录路径'E:\Parent\Child\grant'-Force)
#结果
$NewTree.Name
#结果
#孙子
$NewTree.FullName
#结果
#E:\Parent\Child\孙子
$NewTree.Parent
#结果
$NewTree.PSChildName
#结果
#孙子
获取子项-路径“E:\Parent”-递归
#结果
#因此,迭代子项并设置属性
($ChildFolders=(获取ChildItem-Path'E:\Parent'-Recurse))|
ForEach{$PSItem.Attributes='Hidden'}
#然后设置最后一个父级。
($ParentFolder=Get-ChildItem-Directory-Path'E:\'
其中Name-eq'Parent')。Attributes='Hidden'
获取子项-路径“E:\Parent”-递归
#结果
获取子项-路径“E:\Parent”-强制-递归
#结果

[1]发布代码时,请不要使用别名。它们很难阅读,并且可能不存在于其他系统中。//[2] 请阅读有关如何发布格式化代码的说明。这是降价,所以你可能已经知道了。[grin]////[3]为什么希望在较低级别的目录上设置任何属性都会更改较高级别的目录?[皱眉]所以这开始让我沮丧$f=ni-ItemType Directory-Path'C:[name][name][name]\'-Force$f.attributes='Hidden'我不知道为什么它会全部塞进一行,而“\”都被删除了。。。但到目前为止,我还有一个剧本。李评论的最后一部分是你需要阅读的。您正在将变量设置为完整路径,即该路径中的最后一个目录。如果要更改顶部的属性…需要获取顶部并将其设置在那里。因此,创建您的结构,然后抓取父目录并设置隐藏属性。感谢Lee_Dailey和M0lochwalker。。。让我看看这是否有效。想想什么是$f。从那里开始。[1]发布代码时,请不要使用别名。它们很难阅读,并且可能不存在于其他系统中。//[2] 请阅读有关如何发布格式化代码的说明。这是降价,所以你可能已经知道了。[grin]////[3]为什么希望在较低级别的目录上设置任何属性都会更改较高级别的目录?[皱眉]所以这开始让我沮丧$f=ni-ItemType目录-Path'C:[name][name][name]\'-Force$f.attribute